Using Swagger my API successfully uploads an image from my computer to a folder in my project in Visual Studio . However when I tried uploading an image from my computer to my project folder using the upload button on my web page, I got the error code 400 bad request. I understand that this means my request is malformed among other things. I checked the API URL and it is the exact same URL I used on Swagger and I’ve already cleared my browser cache. I’m not sure what else I need to check as the error is very vague.
Hi, in command prompt I CDed into my solution folder which contains two projects a dotnet core web API project and a dotnet core web project containing only a static web page. Then I created a self signed ssl cert using the following command
dotnet dev-certs https --trust
Unfortunately I still get the 400 bad request error. Also when I pasted the web api endpoint into the browser I got the error
This site can’t be reached
**localhost** refused to connect.
I think it only works using IISExpress or some other server.
That means the port is blocked in IIS. You need to enable it for it to work.
But if you’re running iisexpress and not a windows instance which includes IIS, that is probably your issue. You’ll need to deploy it to hosting (and change the URL since it will no longer be localhost) and try it there.
IISExpress is in Visual Studio and when I run a web page in Visual Studio which makes a post request to the Web API it should just work like it did in past versions but I’m not sure what I need to configure in the version that I’m using. One thing I also don’t understand is if the port I’m using is blocked in IIS how come I was able to successfully upload an image using the API in Swagger which is built into Visual Studio. I’m really frustrated as I can’t even do my development work and test my code.
I finally resolved this issue. It turns out the issue was a minor mistake which was the following line formData.append("inpFile", inpFile.files[0]);
The string “inpFile” which is passed to the append method above as one of its arguments has to match the argument passed to the API controller method in the server side code but it was spelled with an uppercase “F” instead of lowercase “f” and that made the cause of the error hard to spot.