[Vuejs]-Request Body is null in controller?

7πŸ‘

βœ…

.NET isn’t seeing you passing a string, it sees a JSON because you pass the Content-Type header of application/json so it will try to deserialize it and map it to your request object. In your case, because your parameter is string body the parser tries to map the JSON object to your string and fails – so it passes null.

You can try and change the request to pass text/plain as the content-type (or remove the content-type header) or change your API parameter to the object you are sending:

public IActionResult AddNewProduct([FromBody] Product newProduct)
{
    ...
}
πŸ‘€Simply Ged

2πŸ‘

Add to headers Accept

headers: {
    'Content-Type': 'application/json', 
    'Accept': 'application/json',
},
πŸ‘€Sith2021

Leave a comment