What is the Accept-Patch header?
The Accept-Patch response header, defined in Section 3.1 of RFC 5789 alongside the PATCH method, lists the patch document formats a resource accepts. Its value is a comma-separated list of media types, with optional parameters:
Accept-Patch: application/merge-patch+json, application/json-patch+jsonA PATCH body is not the new state of the resource, it is a set of instructions for changing it, and its media type decides how those instructions are read. The same JSON object means something different as a JSON Merge Patch (application/merge-patch+json, RFC 7396) than it does as a JSON Patch (application/json-patch+json, RFC 6902). Accept-Patch is how a client finds out which interpretations the server supports before it sends one.
RFC 5789 gives the header two meanings: the presence of Accept-Patch on a response to any method implies that PATCH is allowed on that resource, and each listed type is one the resource will accept.
API Usage & Best Practices
- Send it on
OPTIONSfor every patchable resource. RFC 5789 says it SHOULD appear in theOPTIONSresponse for any resource that supportsPATCH, next to anAllowheader listingPATCH. - Send it on
415responses. When a client sends a patch format you do not support, answer415 Unsupported Media TypewithAccept-Patchlisting what you do support. RFC 5789 Section 2.2 says such a response SHOULD include it, and it turns a dead end into a one-step fix. - Do not list
application/jsonas a patch format. Plain JSON has no defined patch semantics, so a server that accepts it has invented its own, and a client cannot tell from the header which one. Acceptapplication/merge-patch+jsonfor simple field updates andapplication/json-patch+jsonwhen clients need array operations or tests, and say so explicitly. - Only list a type on a resource that really takes it. Its presence is a promise that
PATCHworks. Do not emit a blanketAccept-Patchon read-only resources or on collections that only takePOST. - Keep it out of
Accept.Acceptis a request header about response formats.Accept-Patchis a response header about request formats. They answer different questions, and its sibling forPOSTisAccept-Post. - Reject malformed patches with
400, unprocessable ones with422. RFC 5789 distinguishes a badly formatted patch document (400 Bad Request) from a valid one that cannot be applied (422 Unprocessable Content), and a failedIf-Matchprecondition from both (412 Precondition Failed). See PATCH strategies.
Examples
Discovering what a resource accepts before patching it:
OPTIONS /v1/users/42 HTTP/1.1Host: api.example.comHTTP/1.1 204 No ContentAllow: GET, PUT, PATCH, DELETE, OPTIONS, HEADAccept-Patch: application/merge-patch+json, application/json-patch+jsonA client sent plain JSON and was told which formats would have worked:
HTTP/1.1 415 Unsupported Media TypeContent-Type: application/problem+jsonAccept-Patch: application/merge-patch+json, application/json-patch+json
{ "type": "https://apiguide.dev/errors/unsupported-media-type", "title": "Unsupported Media Type", "status": 415, "detail": "PATCH bodies must be application/merge-patch+json or application/json-patch+json."}