New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use swagger-ui/cors? #481

Closed
hieubq90 opened this Issue Apr 13, 2016 · 4 comments

Comments

Projects
None yet
4 participants
@hieubq90
Copy link

hieubq90 commented Apr 13, 2016

Problem statement

XMLHttpRequest cannot load http://127.0.0.1:2000/swagger.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

i've tried to add "Access-Control-Allow-Origin" to response header

if origin := r.Header.Get("Origin"); origin != "" {
rw.Header().Set("Access-Control-Allow-Origin", origin)
}
rw.Header().Set("Content-Type", "application/json")
rw.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
rw.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token")
rw.Header().Set("Access-Control-Allow-Credentials", "true")

but not work!
I can't use swagger-ui & swagger_editor to load swager.json & test API

@casualjim

This comment has been minimized.

Copy link
Member

casualjim commented Apr 13, 2016

@casualjim casualjim added the question label Apr 15, 2016

@aroslov

This comment has been minimized.

Copy link

aroslov commented Jun 8, 2016

Working example (in configure_name.go):

import "github.com/rs/cors"

func setupGlobalMiddleware(handler http.Handler) http.Handler {
    handleCORS := cors.Default().Handler

    return handleCORS(handler)
}

@casualjim casualjim changed the title CORS error How to use swagger-ui/cors? Sep 3, 2016

zjs added a commit to zjs/vic that referenced this issue Oct 3, 2017

Vendor github.com/rs/cors v1.1
To include the correct set of CORS headers on each response,
we need to add an appropriate middleware.

This change vendors the middleware recommended by go-swagger
and its transitive dependencies.

This was a mechanical change using the following command:
  gvt fetch -tag v1.1 github.com/rs/cors

See also:
  go-swagger/go-swagger#481

zjs added a commit to vmware/vic that referenced this issue Oct 4, 2017

Vendor github.com/rs/cors v1.1
To include the correct set of CORS headers on each response,
we need to add an appropriate middleware.

This change vendors the middleware recommended by go-swagger
and its transitive dependencies.

This was a mechanical change using the following command:
  gvt fetch -tag v1.1 github.com/rs/cors

See also:
  go-swagger/go-swagger#481

zjs added a commit to zjs/vic that referenced this issue Oct 9, 2017

Vendor github.com/rs/cors v1.1
To include the correct set of CORS headers on each response,
we need to add an appropriate middleware.

This change vendors the middleware recommended by go-swagger
and its transitive dependencies.

This was a mechanical change using the following command:
  gvt fetch -tag v1.1 github.com/rs/cors

See also:
  go-swagger/go-swagger#481

zjs added a commit to vmware/vic that referenced this issue Oct 25, 2017

Vendor github.com/rs/cors v1.1
To include the correct set of CORS headers on each response,
we need to add an appropriate middleware.

This change vendors the middleware recommended by go-swagger
and its transitive dependencies.

This was a mechanical change using the following command:
  gvt fetch -tag v1.1 github.com/rs/cors

See also:
  go-swagger/go-swagger#481

zjs added a commit to vmware/vic that referenced this issue Oct 31, 2017

Vendor github.com/rs/cors v1.1
To include the correct set of CORS headers on each response,
we need to add an appropriate middleware.

This change vendors the middleware recommended by go-swagger
and its transitive dependencies.

This was a mechanical change using the following command:
  gvt fetch -tag v1.1 github.com/rs/cors

See also:
  go-swagger/go-swagger#481
@Sef1995

This comment has been minimized.

Copy link

Sef1995 commented Nov 2, 2017

This is not working for me, I still get 405 Method Not Allowed. Is there a new fix for this?

@casualjim

This comment has been minimized.

Copy link
Member

casualjim commented Nov 3, 2017

405 method not allowed for what? are you sure you're requesting with the right basepath etc?
This solution has been proven to work for many people so something else must be wrong.

@go-swagger go-swagger locked and limited conversation to collaborators Nov 3, 2017

@fredbi fredbi closed this in #1342 Dec 23, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.