Failed to fetch. Possible Reasons: CORS Network Failure URL scheme must be "http" or "https" for CORS request
Asked Answered
M

15

26

I'm trying to make a request to my local API from the Swagger documentation, but it always gives this error when I make the request

enter image description here

Meaningless answered 13/9, 2022 at 13:39 Comment(6)
What's the error message on the Console tab in the browser dev tools?Inmesh
Just got the same problem. Seems my network driver was down or something. Socket couldn't open. After reboot everything was back. No clue about the root cause, I'm interested if you find something or if a reboot helped you tooTapir
Consegui resolver o problema, na verdade o problema estava na própria API que eu estava tentando acessar.Meaningless
@IagoAlexandre What was the problem? I'm still having this same problemStott
I just had this problem. After a few hours trying to figure out what was causing it I found that it was related to a Cookie I was adding to the response. The problem was that the Cookie was too big but there was no error indicating the actual problem.Peridotite
Please share more details, like the error message in text form, along with your attempts to reoslve the problemPimental
C
8

First check that your address is not blocked by cors, for dev tests you can use Access-Control-Allow-Origin:*. If it doesn't work for you, check that you are not using an extension on your browser, such as those that block ads. You delete it from your browser, restart it and test again, it will work.

Channel answered 11/10, 2022 at 13:47 Comment(0)
B
7

In my case, I was accessing the web application via 127.0.0.1:3000, once I changed it to localhost:3000, it worked, hope that helps

Bristol answered 9/11, 2022 at 22:6 Comment(0)
S
3

after Starting IIS Express ... web API register on 2 URL, one port is for http, and the other one is for https. when you call http port you see this error:Failed to fetch. Possible Reasons: CORS Network Failure URL scheme must be "http" or "https" for CORS request. change the port to https Port.

Sochor answered 3/10, 2022 at 10:26 Comment(0)
C
3

In swagger.json I accidently sat my host port to http://localhost:3000 and my server was running on port :5000. Once I updated the host port to match the one I'm using to send my requests using swagger ui, the problem's gone.

Chic answered 15/11, 2022 at 4:56 Comment(0)
S
2

With Open API Swagger Implementation, it worked for me by adding this to main application class.

@OpenAPIDefinition(
  servers = {
    @Server(url = "/myapp/", description = "Default Server URL")
  }
)

This allows both http and https connections without needing to select the scheme explicitly. So, both local (http) and dev server (https) works good.

Skirmish answered 19/9, 2023 at 16:17 Comment(1)
Thanks! It did not fix my issue, but the issue was related to Open API server.Gabey
G
1

Choose the "http" option at the run icon on the top of the screen: enter image description here

Grammer answered 13/3, 2023 at 9:13 Comment(1)
this suggestion could be useful but it is not totally clear what you are setting the http option for. It's helpful to explain external linksSoilasoilage
B
1

A bit of an edge-case situation, but if you are running from a container, ensure you are doing your port forwarding correctly.

enter image description here

As one of the answers above states, your application should be running on localhost:<port_of_choice> instead of 127.0.0.1:<port_of_choice>

Bankruptcy answered 4/4, 2023 at 10:53 Comment(2)
Too, for me it was as simple, as changing the actual port of my backend.Month
Maybe that port was exposed by default.Bankruptcy
H
0

Well it is not that stupid, i had the exact same message and my db was actually not ready ;) I run npm start in the terminal and it works.

Hel answered 3/1, 2024 at 10:52 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Maria
C
0

For all users that also use FastApi, this resolved my issue:

from fastapi.middleware.cors import CORSMiddleware

# Define CORS settings
origins = ["*"]  # Allow requests from any origin

# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
    allow_headers=["*"],
)
Cockerham answered 30/1, 2024 at 10:48 Comment(0)
W
0

I was encounter with same issue -To solve this make sure the port at which your application is running and port mention in server.js (servers URL ) of swagger is same. enter image description here

And then try again enter image description here

Washday answered 19/2, 2024 at 12:53 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewTycoon
R
0

In my case, I solved the issue by changing the site settings of the API that I'm using, in particular by allowing this permission: enter image description here

Regionalism answered 8/4, 2024 at 8:48 Comment(0)
K
0

I use Spring boot, and in my Swagger config i fixed it by changing :

 @Server(
       url = "localhost:8080",
       description = "LOCAL"
 )

to :

 @Server(
     url = "http://localhost:8080",
     description = "LOCAL"
 )

Also declare this Bean in your swagger config :

    @Bean
    public CorsFilter corsFilter() {
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

            CorsConfiguration config = new CorsConfiguration();
            config.setAllowCredentials(true);
            config.addAllowedOrigin("*");
            config.addAllowedHeader("*");
            config.addAllowedMethod("*");

            source.registerCorsConfiguration("/v3/api-docs", config);
            return new CorsFilter(source);
    }
Kerianne answered 22/4, 2024 at 12:25 Comment(0)
L
0

In my case, the API was implemented using Flask and the API itself did not have CORS enabled.

I enabled it by adding following piece of code:

from flask_cors import CORS, cross_origin

app = Flask(__name__)
CORS(app)

PS: you will need to install flask-cors by runnings:

pip install flask-cors
Larkins answered 16/7, 2024 at 18:9 Comment(0)
A
0

I use laravel recently got this same error , So I set this parameter 'APP_URL, APP_WEB_URL, L5_SWAGGER_CONST_HOST' in my env file and then run swagger generate command this resolved my issue. I am adding image for reference enter image description here

Allyn answered 5/8, 2024 at 7:6 Comment(0)
G
-1

I know this is stupid but check if the server is runing

Gomulka answered 2/10, 2023 at 9:58 Comment(1)
Yes, that's stupid: if the server would not be running, the error message would look differentPimental

© 2022 - 2025 — McMap. All rights reserved.