You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # CORS
  2. CORS is a technique to prevent websites from doing bad things with your personal data. Most browsers + JavaScript toolkits not only support CORS but enforce it, which has implications for your API server which supports Swagger.
  3. You can read about CORS here: http://www.w3.org/TR/cors.
  4. There are two cases where no action is needed for CORS support:
  5. 1. swagger-ui is hosted on the same server as the application itself (same host *and* port).
  6. 2. The application is located behind a proxy that enables the required CORS headers. This may already be covered within your organization.
  7. Otherwise, CORS support needs to be enabled for:
  8. 1. Your Swagger docs. For Swagger 2.0 it's the `swagger.json`/`swagger.yaml` and any externally `$ref`ed docs.
  9. 2. For the `Try it now` button to work, CORS needs to be enabled on your API endpoints as well.
  10. ### Testing CORS Support
  11. You can verify CORS support with one of three techniques:
  12. - Curl your API and inspect the headers. For instance:
  13. ```bash
  14. $ curl -I "http://petstore.swagger.io/v2/swagger.json"
  15. HTTP/1.1 200 OK
  16. Date: Sat, 31 Jan 2015 23:05:44 GMT
  17. Access-Control-Allow-Origin: *
  18. Access-Control-Allow-Methods: GET, POST, DELETE, PUT, PATCH, OPTIONS
  19. Access-Control-Allow-Headers: Content-Type, api_key, Authorization
  20. Content-Type: application/json
  21. Content-Length: 0
  22. ```
  23. This tells us that the petstore resource listing supports OPTIONS, and the following headers: `Content-Type`, `api_key`, `Authorization`.
  24. - Try swagger-ui from your file system and look at the debug console. If CORS is not enabled, you'll see something like this:
  25. ```
  26. XMLHttpRequest cannot load http://sad.server.com/v2/api-docs. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
  27. ```
  28. Swagger-UI cannot easily show this error state.
  29. - Using the http://www.test-cors.org website. Keep in mind this will show a successful result even if `Access-Control-Allow-Headers` is not available, which is still required for Swagger-UI to function properly.
  30. ### Enabling CORS
  31. The method of enabling CORS depends on the server and/or framework you use to host your application. http://enable-cors.org provides information on how to enable CORS in some common web servers.
  32. Other servers/frameworks may provide you information on how to enable it specifically in their use case.
  33. ### CORS and Header Parameters
  34. Swagger lets you easily send headers as parameters to requests. The name of these headers *MUST* be supported in your CORS configuration as well. From our example above:
  35. ```
  36. Access-Control-Allow-Headers: Content-Type, api_key, Authorization
  37. ```
  38. Only headers with these names will be allowed to be sent by Swagger-UI.