How to use axios.put to send JSON to ASP.NET controller

Viewed 328

I'm trying to send a PUT request with JSON data using the following client code:

const url = new URL(`${process.env.REACT_APP_API}/datas/edit/${id}`);
axios.put(url, data);

And on the server side, when I'm trying to look at in the HttpRequest.Form, the Controller throws InvalidOperationException exception . The message is Incorrect Content-Type: application/json;charset=UTF-8.

[HttpPut("edit/{id}")]
public void Edit([FromRoute] int id, [FromBody] Data data)
...

I also tried axios.put(url, JSON.stringify(data)); but server returns 415.

EDIT: I tried with Postman instead of my front-end:

enter image description here

public class A { public int /*or string*/ A1 { get; set; } }
[HttpPut("edit/{id}")]
public void EditQuestion([FromRoute] int id, [FromBody] A a) ...

IMPORTANT:

I shouldn't have looked at HttpRequest.Form because I'm sending JSON data and it should be parsed into my model.

1 Answers

I see the json response from postman returns a string, instead your "A" class has an int property for A1.

Try changing the class to:

public class A { public string A1 { get; set; } }
Related