Swagger-ui: Cors issue with try it now button

Created on 28 Nov 2019  路  5Comments  路  Source: swagger-api/swagger-ui

When i hit try it now button getting cors issue and failed to fetch in response code. I tried to add cors headers in server side but still my problem is not solved. I am using authorization mechanism, Here when i authorised it will add a header name of token, So browser is calling options call. Showing error is Options method not allowed.

Please suggest me any other solution. Like from swagger UI etc.

Most helpful comment

I'm currently proxying it for a demo I'm doing if that's any help.

<SwaggerUI
          docExpansion="full"
          url="/spec.json"
          requestInterceptor={(req) => {
            // Proxy CORS requests
            if (req.url.includes('http')) {
              req.url = `https://cors-anywhere.herokuapp.com/${req.url}`;
            }
            return req;
          }}
        />

All 5 comments

The OPTIONS call is part of the CORS process. If you're getting the error, then your CORS is not set up properly.

@webron can you give me other solution's for this because i am not able change anything on my server side.

i.e is there any possibility to disable cors from swagger ui.

CORS check is a browser security check, we can't do anything about it. You can disable it in your browser (which is highly not recommended) and you'd have to search online how to do so based on the browser you use. Another option is to use a CORS proxy.

I'm currently proxying it for a demo I'm doing if that's any help.

<SwaggerUI
          docExpansion="full"
          url="/spec.json"
          requestInterceptor={(req) => {
            // Proxy CORS requests
            if (req.url.includes('http')) {
              req.url = `https://cors-anywhere.herokuapp.com/${req.url}`;
            }
            return req;
          }}
        />

@gavmck Thank you. It is working fine.

Was this page helpful?
0 / 5 - 0 ratings