Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i use query parameters with Http Patch method?

Tags:

rest

Can i use query parameters in http PATCH method in my Rest API, instead of sending a partial request body?

  1. I want to avoid the overhead of maintaining the request class in the sender and receiver.

  2. I want to update only 1 field.

like image 903
user1354825 Avatar asked Jul 20 '26 11:07

user1354825


1 Answers

Can i use query parameters in http PATCH method in my Rest API, instead of sending a partial request body?

Yes, but probably not the way that you mean.

PATCH /a?b=c HTTP/2.0
Content-Type: application/merge-patch+json

{ "y": "z"}

is a perfectly satisfactory HTTP request. It says "find the document identified by /a?b=c, and to that document apply the merge-patch as specified by RFC 7386."

Note carefully: the "query parameters" here are part of the identification of which document the patch should be applied to, and do not in any way describe the patch that should be applied.


What you should not be doing:

PATCH /a?b=c

as an attempt to modify /a by updating "b" to the value "c". The fundamental problem here is that you are creating a new meaning for a request that has a standardized meaning, and therefore there is likely to be confusion when somebody tries to access your API using a general-purpose component.

In a situation where your API is only used by front ends that you control (for example, only called via your java script client downloaded from your web servers), and if you don't need to use any intermediate components (like a web cache) in the middle, then you might get away with it.

If you review HTTP cache invalidation, you will see that PATCH /a?b=c does not invalidate cached representations fetched via GET /a, because the target-uri do not match.

like image 54
VoiceOfUnreason Avatar answered Jul 26 '26 06:07

VoiceOfUnreason