Skip to content

Instantly share code, notes, and snippets.

@vicly
Created March 20, 2019 20:32
Show Gist options
  • Select an option

  • Save vicly/c765f1587db2c6610a911db31fd7b750 to your computer and use it in GitHub Desktop.

Select an option

Save vicly/c765f1587db2c6610a911db31fd7b750 to your computer and use it in GitHub Desktop.
[PUT vs PATCH] #REST

Key:

  • Don't use PATCH OR use it the right way
  • PATCH is not to replace POST or PUT
  • PATCH applies a delta rather than replacing the entire resource
  • PATCH is of a different content-type
  • PUT's payload is considered to be a modified version of the resource stored on the origin server, so the client is to replace it
  • PATCH's payload contains a set of instructions about how to modify the resource on the origin server, so to produce a new version
    • You can use whatever format you want as “description of changes”
    • That’s why using PATCH to send updated values only is not suitable.
// not correct
PATCH /users/123
{ “email”: "new.email@example.org” }


// not correct
PATCH /users/123?email=new.email@example.org

// correct
PATCH /users/123
[ 
  { "op": "replace", "path": "/email", "value": "new.email@example.org" }
]
  • RFC 7397: JSON Merge Patch, a "just send what you need" format

  • RFC 6902: JSON Patch, JSON Patch defines a JSON document structure for expressing a sequence of operations to apply to a JSON document.

    • application/json-patch+json
  • RFC 6901: JSON Pointer defines a string syntax for identifying a specific value within a JSON document.

  • RFC 5261: XML patch framework.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment