Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting Null on Patch Requests #66

Closed
amarwadi opened this issue Apr 28, 2017 · 6 comments
Closed

Getting Null on Patch Requests #66

amarwadi opened this issue Apr 28, 2017 · 6 comments

Comments

@amarwadi
Copy link

Hello,
Is there anything special I need to do other than adding the Nuget Package and using the JsonPatchDocument on the [FromBody] attribute for the HttpPatch methods on Web API? For some reason, no matter what I do, I always get a null object. Here's how my call looks like:

     [Route("sites/{siteId}/affiliates/{affiliateId}/codes/{codeId}/commissions/{id}"), HttpPatch,
     ResponseType(typeof(BoolResult)), ValidateSiteId()]
    public async Task<IHttpActionResult> AddTieredCommission(long siteId, long affiliateId, string codeId,
       string id, [FromBody] JsonPatchDocument<AddTieredCommissionModel> model)
    {
        
        return Ok();
    }

I am using ASP.NET Web API (not on ASP.NET Core). Any help would be appreciated.

Anup

@LouisChiero
Copy link

Are you setting the request's Content-Type as "application/json-patch+json"?

@Swoogan
Copy link

Swoogan commented May 26, 2017

I had this same issue. Make sure that the JSON body of your request is formatted correctly. I was using RestSharp, which serializes using a formatter that doesn't respect the [SerializeAs] attributes on JsonPatchDocument. Using fiddler I was able to see what was being passed in the request body and with Insomnia I could confirm that it didn't work. By serializing with Newtsoft.Json, I got out put that looked like the json patch RFC.

@KevinDockx
Copy link
Owner

Closing issue as it seems to be solved. If you experience additional problems, feel free to reopen.

@dpescatore
Copy link

How this was resolved? I have the same problem.
I've tried adding
config.Formatters.JsonFormatter.SupportedMediaTypes.Add( new MediaTypeHeaderValue("application/json-patch+json"));
And both with simple "application/json" but I always have a null object on server.

Here's signature of my api method

public IHttpActionResult PatchEntity(int id, [FromBody]JsonPatchDocument<Entity> entity)

and here my simple JSONPatch sent by restlet client.

{ "op": "replace", "path": "/seconds", "value": "12" }

Any help on this?

Thanks,
Daniele

@dpescatore
Copy link

I solved by myself.
A correct JSONPatch body must contain an array of operations.
Changing my request in:

[{ "op": "replace", "path": "/seconds", "value": "12" }]

works like a charm.

Daniele

@jvargasvillegas
Copy link

Thanks.
I was doing same mistake

Bad code
{
{
"op": "replace",
"path": "/name",
"value": "Updated - Central Park"
}
}

Fixed code
[
{
"op": "replace",
"path": "/name",
"value": "Updated - Central Park"
}
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants