Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger UI - TypeError: Failed to fetch - on endpoint request (ASPNET Core API)

When trying to run a request through swagger UI, I receive the following response on Swagger

TypeError: Failed to fetch

After searching around, I found that a possible cause of this error is because of a CORS issue, where the origin is changed in the request (as you can see at this other post here). However, in my case, this is not running through some other proxy, it is hosted on a locally hosted server and that server is not changing any of the headers. I realized this when I tried to allow the API to just accept any CORS headers to test if this was the issue; sadly it was not and the issue persisted.

The API is running on IIS hosted on a server that is hosted locally. The API is running as an application on the default website and is accessed via the following url:

http://servername/application-name/swagger/index.html

Can anyone help with this issue?

like image 235
Newteq Developer Avatar asked Dec 06 '19 08:12

Newteq Developer


1 Answers

After further investigation, I found that when I looked at the requests being sent to the server through the dev tools on the browser, that the URL was being changed from http to https on the request of the endpoint through swagger.

HTTPS, has not been set up on the server and returns a 404 (as seen in the dev tools).

It turns out, that even though the server has not been setup to serve content via HTTPS, the requests where still redirected to HTTPS and this was the reason

app.UseHttpsRedirection();

So, even though swagger was able to be loaded on HTTP, when the request was made to the API, the API responded with a 307 - for redirection and the request was redirected to HTTPS - which in turn returned 404. This 404 response was the cause the TypeError: Failed to fetch

The recommended fix for this is to turn off https redirection (ONLY FOR TESTING PURPOSES) and the other is to enable the server to serve the content correctly over HTTPS, so that when a call is made, it is not redirected, but rather sent straight to the correct API address on HTTPS - which should not return the data correctly, since the server can serve HTTPS content

like image 167
Newteq Developer Avatar answered Oct 12 '22 22:10

Newteq Developer