Using dotnet 8, Asp.net Core api, I tried setting up a simple toy example of an API server with some endpoints. Client side, it’s a simple React app that queries the endpoints with a fetch request. I don’t understand why, but POST/PUT/PATCH endpoints whose methods take input are not getting the data in the request body. It’s coming in as null. My example code is below.
I don’t have this issue with GET endpoints where the associated methods have inputs. If I change the input type to dynamic instead of string, then it is able receive the request body.
What I tried:
Server endpoint example:
[HttpPost]
[Route("simplesubmit")]
public void SimpleSubmit([FromBody] string dataAsString){
// simple work
}
Client fetch example:
await fetch ("urlgoeshere.com/simplesubmit", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
name: "my name"
})
});
What happens:
The SimpleSubmit method isn’t called, and there’s a network response of 400 indicating that the dataAsString field is required.