Nikita128
Nikita128

Reputation: 462

Is it okay to use HTTP POST method with a query string, if a query string is used to find the resources that need to be updated?

My HTTP API has a method that updates resources found by filter. Practically it is a POST method, that utilizes a query string to find desired resources and transfers data in its body to update found resources:

POST /module/resources?field_1=abc&field_2=def&field_n=xyz
body: {
    "desired_field":"desired_data"
}

I've heard it might be a code smell using a POST method and a query string together, but in the case presented above it seems perfectly reasonable to me.

Am I making wrong assumptions here?

Upvotes: 0

Views: 82

Answers (2)

VoiceOfUnreason
VoiceOfUnreason

Reputation: 57239

Is it okay to use HTTP POST method with a query string, if a query string is used to find the resources that need to be updated?

Yes.

Am I making wrong assumptions here?

No.

The key ideas being that you, as the author of the resource, get to choose its identifier

REST relies instead on the author choosing a resource identifier that best fits the nature of the concept being identified. -- Fielding, 2000

the query is part of the resource identifier

The query component contains non-hierarchical data that, along with data in the path component (Section 3.3), serves to identify a resource within the scope of the URI's scheme and naming authority -- RFC 3986

and that the semantics of HTTP methods are standardized across all resources

Once defined, a standardized method ought to have the same semantics when applied to any resource, though each resource determines for itself whether those semantics are implemented or allowed. -- RFC 7231

Upvotes: 1

Evert
Evert

Reputation: 99533

When talking about resources, there's from a HTTP/REST perspective no difference between:

/article/1
/article?id=1

So if you do a GET request on either of these to get the article, you can do a PUT or PATCH on either of those to make changes.

However, the way many developers think of query parameters and POST bodies is often 'just a different way to send parameters'. This is incorrect, because they have a pretty distinct meaning, but using both at the same time may confuse some people.

So on a protocol level what you're doing is perfectly fine. I'd argue it's kind of elegant to use the URI as the locator and the body as the main message.

Upvotes: 1

Related Questions