Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to include data with an HTTP PATCH request

When I'm putting together an HTTP PATCH request, what are my options to include data outside of URL parameters?

Will any of the following work, and what's the most common choice?

  • multipart/form-data
  • application/x-www-form-urlencoded
  • Raw JSON
  • ...any others?
like image 262
Alexander Trauzzi Avatar asked Jun 29 '13 01:06

Alexander Trauzzi


2 Answers

The PATCH method is defined in the RFC 5789. This document, however, doesn't enforce any media type for the payload:

The PATCH method requests that a set of changes described in the request entity be applied to the resource identified by the Request-URI. The set of changes is represented in a format called a "patch document" identified by a media type.

Other RFCs, released years later, define some media types for describing a set of changes to the applied to a resource, suitable for PATCHing:

application/json-patch+json

Defined in the RFC 6902:

JSON Patch defines a JSON document structure for expressing a sequence of operations to apply to a JavaScript Object Notation (JSON) document; it is suitable for use with the HTTP PATCH method. The application/json-patch+json media type is used to identify such patch documents.

application/merge-patch+json

Defined in the RFC 7396:

This specification defines the JSON merge patch format and processing rules. The merge patch format is primarily intended for use with the HTTP PATCH method as a means of describing a set of modifications to a target resource's content.

like image 118
cassiomolin Avatar answered Oct 15 '22 22:10

cassiomolin


There are no restrictions on the entity bodies of HTTP PATCH requests as defined in RFC 5789. So in theory, your options in this area are unlimited.

In my opinion the only sensible choice is to use the same Content-Type used to originally create the resource. The most common choice is application/json simply because most modern APIs utilize JSON as their preferred data transfer format.

The only relevent statement RFC 5789 makes in regard to what should and shouldn't be part of your PATCH entity body is silent on the matter of Content-Type:

the enclosed entity contains a set of instructions describing how a resource currently residing on the origin server should be modified to produce a new version.

In summary, how you choose to modify resources in your application is entirely up to you.

like image 40
rdlowrey Avatar answered Oct 16 '22 00:10

rdlowrey