KevinDockx / JsonPatch

JSON Patch (JsonPatchDocument) RFC 6902 implementation for .NET
MIT License
173 stars 29 forks source link

Getting Null on Patch Requests #66

Closed amarwadi closed 7 years ago

amarwadi commented 7 years ago

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 commented 7 years ago

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

Swoogan commented 7 years ago

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 commented 7 years ago

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

dpescatore commented 6 years ago

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 commented 6 years ago

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 commented 5 years ago

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" } ]