Accept x-www-form-urlencoded in Web API .NET Core
Try using [FromForm]
instead of [FromBody]
.
public IActionResult Post([FromForm] PlayerPackage playerPackage)
FromBody
> Bind from JSONFromForm
> Bind from Form parameters
You can also remove [FromBody]
altogether and trial it then. Because you are expecting form-urlencoded should tell it to bind to object.
This did the trick for me:
[HttpPost]
[Consumes("application/x-www-form-urlencoded")]
public IActionResult Post([FromForm]IFormCollection value)
For PlayerPackage
, the request should send a PlayerPackage Json Object, based on your description, you could not control the request which is posted from other place.
For the request, its type is application/x-www-form-urlencoded, it will send data with {"status":"incomplete","score":""} in string Format instead of Json object. If you want to accept {"status":"incomplete","score":""}, I suggest you change the method like below, and then conver the string to Object by Newtonsoft.Json
[HttpPost]
[Route("~/api/trackAllInOne/set")]
[Consumes("application/x-www-form-urlencoded")]
public IActionResult Post([FromForm] string data)
{
PlayerPackage playerPackage = JsonConvert.DeserializeObject<PlayerPackage>(data);
return Json(data);
}