I have a .NET Core Web API that is returning a 415 Unsupported Media Error when I try to post some data to it that includes some json. Here's part of what is returned in the Chrome Debugger:
Request URL:http://localhost:51608/api/trackAllInOne/set
Request Method:POST
Status Code:415 Unsupported Media Type
Accept:text/javascript, text/html, application/xml, text/xml, */*
Content-Type:application/x-www-form-urlencoded
action:finish
currentSco:CSharp-SSLA:__How_It_Works_SCO
data:{"status":"incomplete","score":""}
activityId:13
studentId:1
timestamp:1519864867900
I think this has to do with my controller not accepting application/x-www-form-urlencoded
data - but I'm not sure. I've tried decorating my controler with Consumes
but that does not seem to work.
[HttpPost]
[Route("api/trackAllInOne/set")]
[Consumes("application/x-www-form-urlencoded")]
public IActionResult Post([FromBody] PlayerPackage playerPackage)
{ etc..}
Any help greatly appreciated.
The following code worked fine in .NET 4.6.1 and I am able to capture and process the posts shown above.
[ResponseType(typeof(PlayerPackage))]
public async Task<IHttpActionResult> PostLearningRecord(PlayerPackage playerPackage)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var id = Convert.ToInt32(playerPackage.ActivityId);
var learningRecord = await _context.LearningRecords.FindAsync(id);
if (learningRecord == null)
return NotFound();
etc...
PlayerPackage
? How did you send the request from Front? – Edwarddata:{"status":"incomplete","score":""}
or you want to log all the bodyaction:finish currentSco:CSharp-SSLA:__How_It_Works_SCO data:{"status":"incomplete","score":""} activityId:13 studentId:1 timestamp:1519864867900
– Edward[ResponseType(typeof(PlayerPackage))] public async Task<IHttpActionResult> PostLearningRecord(PlayerPackage playerPackage) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var id = Convert.ToInt32(playerPackage.ActivityId); var learningRecord = await _context.LearningRecords.FindAsync(id); if (learningRecord == null) return NotFound();
– Roddy Balkan