I'm designing an API to go over HTTP and I am wondering if using the HTTP POST command, but with URL query parameters only and no request body, is a good way to go.
Considerations:
Content-Length: 0
header must be explicitly added.Are there any more pitfalls or advantages to sending parameters on a POST request via the URL query rather than the request body?
Edit: The reason this is under consideration is that the operations are not idempotent and have side effects other than retrieval. See the HTTP spec:
In particular, the convention has been established that the GET and HEAD methods SHOULD NOT have the significance of taking an action other than retrieval. These methods ought to be considered "safe". This allows user agents to represent other methods, such as POST, PUT and DELETE, in a special way, so that the user is made aware of the fact that a possibly unsafe action is being requested.
...
Methods can also have the property of "idempotence" in that (aside from error or expiration issues) the side-effects of N > 0 identical requests is the same as for a single request. The methods GET, HEAD, PUT and DELETE share this property. Also, the methods OPTIONS and TRACE SHOULD NOT have side effects, and so are inherently idempotent.
POST should not have query param. You can implement the service to honor the query param, but this is against REST spec.
Website URL parameters are commonly used for tracking session IDs, product category page filters, powering search queries and more. Parameters can be valuable but do confuse search engines, resulting in page indexing issues and wasted crawl budget.
Parameters Make URLs Less Clickable They're hard to read. They don't seem as trustworthy. As such, they are less likely to be clicked. This will impact page performance.
URL parameters can potentially cause a lot of problems when it comes to your SEO. For example, they can create duplicate content, waste crawl budget, and dilute ranking signals.
If your action is not idempotent, then you MUST use POST
. If you don't, you're just asking for trouble down the line. GET
, PUT
and DELETE
methods are required to be idempotent. Imagine what would happen in your application if the client was pre-fetching every possible GET
request for your service – if this would cause side effects visible to the client, then something's wrong.
I agree that sending a POST
with a query string but without a body seems odd, but I think it can be appropriate in some situations.
Think of the query part of a URL as a command to the resource to limit the scope of the current request. Typically, query strings are used to sort or filter a GET
request (like ?page=1&sort=title
) but I suppose it makes sense on a POST
to also limit the scope (perhaps like ?action=delete&id=5
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With