Proper data format when sending form data from client to server

asp.net-core asp.net-core-webapi c# entity-framework-core reactjs

Question

I'm confused as to how I should be sending form data from a React front-end form to a .NetCore Entity Framework controller.

It seems I either get a 400 or a 415 error.

Here is how I am sending the data in my front-end:

axios({
        method: "POST",
        url: "https://localhost:44376/api/formdata",
        data: JSON.stringify(values),
        headers: { 'Content-Type': 'application/json; charset=utf-8' }
});

And here is how I'm recieving it:

[HttpPost]
public async Task<ActionResult> AcceptContractor([FromBody] CreateContractor data)
{ .. }

And here is what the data looks like written out to the console in Chrome:

{
  "jobIds": [
    0,
    "2",
    "3"
  ],
  "email": "alljobs@xyz.com",
  "name": "Jim Smith",
  "phone": "100-200-3000",
  "jobType": "Residential",
  "title": "July Jobs",
  "startDate": "2020-07-23T15:50:29.345Z",
  "finishDate": "2020-09-03T04:00:00.000Z"
}

I'm wondering maybe I shouldn't be using 'FromBody'?

Thanks!

1
0
3/23/2020 4:20:32 PM

Popular Answer

Test this curl in postman :

curl --location --request POST 'https://localhost:44376/api/formdata' \
--header 'accept: */*' \
--header 'Content-Type: application/json-patch+json' \
--data-raw '{
    "jobIds": [
        "0",
        "2",
        "3"
    ],
    "email": "alljobs@xyz.com",
    "name": "Jim Smith",
    "phone": "100-200-3000",
    "jobType": "Residential",
    "title": "July Jobs",
    "startDate": "2020-07-23T15:50:29.345Z",
    "finishDate": "2020-09-03T04:00:00.000Z"
}'
1
3/23/2020 4:35:20 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow