Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

ASP.NET Core Web API - POST JSON, no Data Passed #7257

@Eilon

Description

@Eilon

From @jkergosi on January 17, 2018 23:46

I have an existing web api call in .NET 4.7, when brought to .NET Core 2, doesn't work. I'd updated my code to be 2.0 compliant, but when I POST, the JObject parameter is not populated.

[Produces("application/json")]
[Route("api")]
public class MyController : Controller
{
        [HttpPost("Create")]        
        public async Task<HttpResponseMessage> Create([FromForm] JObject data)
        {            
            if (data == null)
                return new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, ReasonPhrase = "POST body is null" };

            try
            {
                await db.CreateItemAsync(data);
                return new HttpResponseMessage { StatusCode = HttpStatusCode.OK, ReasonPhrase = "Saved" };
            }
            catch (Exception ex)
            {
                return new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError, ReasonPhrase = $"Document could not be created: {ex.InnerException}" };
            }
        }
}
The JQuery call is:
function SaveDocument() {
    
    var myData = JSON.stringify({ "userName": "bob", "password": "123" });

    $.ajax({
        url: url,
        type: "POST",
        contentType: "application/json",
        dataType: "json",
        data: myData,

        success: function (result) {
            console.log(result);
        },

        error: function (xhr, resp, text) {
            console.log(xhr, resp, text);
        }
    });
}

The Create method is called, but the JObject parameter is empty:

results

I've also tried dynamic instead of JObject, but no dice.

Any suggestions? Thanks!

Copied from original issue: dotnet/aspnetcore#2793

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions