Send a list of strings to an ASP.net core api controller with vanilla Javascript
This might sound simple but something is going wrong and i'm not sure what. I am trying to send a list of strings such as [ "something", "something else", "Another string" ] to my api post controller in ASP.Net Core.
My Controller looks like this
[HttpPost]
public ActionResult<MyModel> Name([FromBody] List<string> list)
{
// Do something...
return NoContent();
}
And my Javascript looks like this
async function apiCall() {
const response = await fetch("URL", {
method: 'POST',
headers: {
'Content-type': 'application/json',
'X-CSRFToken': csrftoken
},
body: JSON.stringify({ list: [ "something", "something else", "Another string" ] })
}}
When ever I call the apiCall() funcation it posts data to the controller but the data List is always null. How do I correctly post this data?
body: JSON.stringify([ "something", "something else", "Another string" ])It works!