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
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.
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
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.
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.
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.
server
. –
Gabey Choose the "http" option at the run icon on the top of the screen:
A bit of an edge-case situation, but if you are running from a container, ensure you are doing your port forwarding correctly.
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>
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.
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=["*"],
)
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.
And then try again
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);
}
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
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
I know this is stupid but check if the server is runing
© 2022 - 2025 — McMap. All rights reserved.