1

I am insecure whether to use an combination of POST & PATCH or if it is better to only offer PUT requests for my use-case.

If I am creating the following resource:

POST /customer
firstname: John
lastname: Smith
email: j.smith@web.com
(response -> customer created with id '1')

Can a PATCH request be used afterwards to update one existing value (email in this case) and also add new fields and values (birthdate) to the resource?

PATCH /customer/1
firstname: John
lastname: Smith
email: johnny.smith@gmail.com
dateofbirth: 1970-01-01

dateofbirth would be already part of the swagger definition in this case.

  • 1
    In short, yes. Though PATCH is probably not what you think it is. It is quite similar to traditional patches used in software development where a patch document contains instructions that a target machine has to apply fully in order to alter a resource to its desired outcome. [Suggested reading](https://williamdurand.fr/2014/02/14/please-do-not-patch-like-an-idiot/) – Roman Vottner Feb 05 '21 at 10:32
  • Your question makes it sound as if you are sending the data with request header fields. That's not a good idea. – Julian Reschke Feb 05 '21 at 11:30
  • @JulianReschke. Data will be within the body. It was just a very simplified representation – apidesigner90 Feb 05 '21 at 13:34

1 Answers1

2

Can a PATCH request be used afterwards to update one existing value (email in this case) and also add new fields and values (birthdate) to the resource?

Yes. If you can describe the changes you want to make in a patch document, then you can use PATCH.

You are not required to support PUT on all resources that support PATCH, but PATCH support without PUT support is a bit odd, given the similarity in the method semantics.

You will normally want to use a standardized patch document, rather than rolling your own. For example, with application/json documents, you'll normally want to support application/merge-patch+json, application/json-patch+json or both.

Finding a good patch document representation for plain text files is harder.

Community
  • 1
  • 1
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91