2

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?

Newteq Developer
  • 1,038
  • 14
  • 27
  • Check your actions. This problem had already happened to me. One of the actions was not [HttpPost] (or get, put , ....) – Hamed Hajiloo Dec 06 '19 at 09:33
  • @HamedHajiloo The actions were correct, I thought that something weird was going on, because I thought that it was running a GET into of the POST that we were requesting, but it turns out it was the HTTPS redirect that was causing the problem in our case – Newteq Developer Dec 06 '19 at 09:38

1 Answers1

3

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

Newteq Developer
  • 1,038
  • 14
  • 27