KevinDockx / JsonPatch

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

application/json-patch+json MediaTypeFormatter? #49

Closed ericmaia closed 8 years ago

ericmaia commented 8 years ago

Maybe I'm missing something obvious... I installed this package in my Web Api 2 project using the NuGet package (0.9.0) and I am able to use it to apply PATCH changes. However, WebApi complained that there was no MediaTypeFormatter for "application/json-patch+json" - but it worked fine when I changed the Content-Type to "application/json"

{ "Message": "The request entity's media type 'application/json-patch+json' is not supported for this resource.", "ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'JsonPatchDocument 1' from content with media type 'application/json-patch+json'.", "ExceptionType": "System.Net.Http.UnsupportedMediaTypeException", "StackTrace": " at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable 1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable 1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable 1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)" }

Do I need to do something to add the MediaTypeFormatter in the WebApiConfig? I didn't see anything about this in the docs. (I'm also not sure if this is really an issue, or if I should post this kind of question on StackOverflow.)

KevinDockx commented 8 years ago

Hi @ericmaia, your guess is right. You'll have to add application/json-patch+json as a supported media type for the JsonFormatter:

config.Formatters.JsonFormatter.SupportedMediaTypes.Add( new MediaTypeHeaderValue("application/json-patch+json"));

FYI: to fully follow the standard, patch requests should always be sent with application/json-patch+json - but a lot (most) API's also allow application/json for those types of requests.