Next Developer
Next Developer

Reputation: 1229

Is it acceptable to pass json string in the http header to provide options to the server when doing PATCH request?

In my restful service, I want to allow users to update certain fields of a resource with PATCH requests. However, there's a requirement that when updating certain fields I have to perform certain actions on the back-end depending on the provided options. I don't want to mix the options with the primary data in the body of the request, so I came up with 2 possible solutions.

  1. Pass the options via query string
  2. Pass the options via http header as json string.

Since, in some cases the options can contain some relatively large text, I decided to pass the options via http header.

Has anybody done this before? Are there any possible issues I can face later? Is it good practice at all? If not, how else I can accomplish the same?

Upvotes: 1

Views: 1260

Answers (1)

Pedro Werneck
Pedro Werneck

Reputation: 41898

I'd say that depends on how much of a REST purist you want to be.

I'd prefer to pass the options with the body, because the PATCH method has clearly defined semantics, but no defined data format, so, there's nothing preventing you from sending the options in the PATCH body, since you have to document the payload format anyway.

If that's not an option for you, I'd say passing the options via query string is the least desirable one, since URIs are atomic, including the query string, and you're effectively changing the identifier.

Using a custom HTTP header is more acceptable, but you must document how exactly that header is supposed to work. A minor issue is that header length is undefined by the HTTP protocol, but the limit is at least a few KB for most implementations, so this is probably not a real problem.

Upvotes: 1

Related Questions