简体   繁体   中英

how to convert the JSON object into form data and send it as Data to web api fetch

I am having a json object in the client side and i want to receive that in the Web api controller like controllerContext.HttpContext.Request["Name"] How to send that Json from web API.

The Data expected as "name=abc&type=xyz" for the fetch api. How to convert the JSON object

 { name: abc, type:xyz }

below code is not working

 { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json' }, credentials: 'same-origin', body: JSON.stringify({ name:abc, type:xyz }) };
Replacing body and header as below works fine

 body: "name=abc&type=xyz" headers: new Headers({ 'Content-Type': 'application/x-www-form-urlencoded', // <-- Specifying the Content-Type }

How to convert the Json object as above and what is the reason for that .?

I don't know ASP.NET but I believe there's a way out there you can configure your data type to JSON instead of form-urlencoded. Or you need to customize how server parsing data.

If you can not change to application/json. You can use querystring package to stringify your JSON obj into urlenconded format.

Can you share your .NET code ?

Request Body doesn't take value like Params!

Body should take data from JSON.

You can use [FromBody] attribute! or can create a DTO for this. DTO should take care of this automatically!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM