Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a HTTP PATCH request create an resource?

If a PATCH request is applied to a resource that doesn't exist yet, is it then allowed to create the resource or do I need a separate POST/PUT request in that case?

The PATCH request would go to the URL for the resource for example: PATCH /object/1234. If the object with the ID 1234 is in the database I'll create it, otherwise I'll update it.

The PATCH request doesn't contain all fields, that's why I don't use PUT.

like image 206
Jimmy T. Avatar asked Dec 23 '16 18:12

Jimmy T.


2 Answers

RFC 5789 states that PATCH should be used "to modify an existing HTTP resource." It would probably be best to implement a POST/PUT request in order to adhere to the HTTP standards.

like image 94
Acontz Avatar answered Sep 28 '22 16:09

Acontz


Whilst a server can create a new resource (as explained in Heiko's answer), you'd be advised to only implement conditional PATCH, where a client sends an If-Unmodified-Since or If-Match header, ensuring the patch is applied only to the version of the resource the client thought they were editing. Conditional PATCH requests preclude the idea of editing a non-extant resource. If a client attempts to send a request without a precondition, the correct response is 428 Precondition Required. See RFC 6585.

like image 33
Nicholas Shanks Avatar answered Sep 28 '22 18:09

Nicholas Shanks