Reputation: 7959
I'm trying to send an AJAX PATCH request to a Web API method and have the patched object recognised by Marvin.JsonPatch.
So far, everything I've sent to the server has resulted in an empty request being received.
The Web API controller method looks like this:
public async Task<IHttpActionResult> Update(long orderId, JsonPatchDocument<Order> patchedOrder)
I'm POSTing using an HttpClient
like this (can't use async
in this application)...
var patchDoc = new JsonPatchDocument<Order>();
patchDoc.Replace(e => e.Price, newPrice);
patchDoc.Replace(e => e.WordCount, newWordCount);
var request = new HttpRequestMessage(new HttpMethod("PATCH"), uri)
{
Content = new StringContent(JsonConvert.SerializeObject(patchDoc),
System.Text.Encoding.Unicode,
"application/json")
};
HttpResponseMessage response;
using (var client = new HttpClient(...))
{
response = client.SendAsync(request).GetAwaiter().GetResult();
}
But when the controller is it, the patchedOrder
argument is null
.
While debugging on the controller I've also tried
var s = await Request.Content.ReadAsStringAsync();
But this returns an empty string - can anyone explain why?
UPDATE:
This is what the contents of the JsonPatch document look like when passed in to the HttpClient...
{
"Operations": [{
"OperationType": 2,
"value": 138.7,
"path": "/price",
"op": "replace"
},
{
"OperationType": 2,
"value": 1320,
"path": "/wordcount",
"op": "replace"
}],
"ContractResolver": {
"DynamicCodeGeneration": true,
"DefaultMembersSearchFlags": 20,
"SerializeCompilerGeneratedMembers": false,
"IgnoreSerializableInterface": false,
"IgnoreSerializableAttribute": true,
"NamingStrategy": null
},
"CaseTransformType": 0
}
Upvotes: 0
Views: 1209
Reputation: 151738
Somewhere during the development of Marvin.JsonPatch, the JsonPatchDocument<T>
was annotated with an attribute that applied a custom JSON serializer:
[JsonConverter(typeof(JsonPatchDocumentConverter))]
This converter enables you to call JsonConvert.SerializeObject()
on such a patch document and actually generate a patch document, as opposed to a representation of the JsonPatchDocument<T>
CLR object.
Upgrade Marvin.JsonPatch and Newtonsoft.Json to the latest verison, and serialization should succeed.
Upvotes: 1