I came up with a situation which I would like to resolve using HTTP standard and best practices of building web application. The legacy application which I'm trying to rewrite allows an HTTP POST request with payload and input parameter(s). For clarity, here is an example of curl call:
# place HTTP POST request with some payload and input parameter
curl -X POST -H "Content-Type: application/json" [email protected] http://host.com/api?q=1
So, here we have a payload presented in data.json
file (as valid JSON), and at the same time, the API allows to pass input parameter q=1
. This API internally relies on the value of this parameter to perform some logic with payload data. My question here is if it is acceptable from HTTP standard point of view, or it is better to redesign the API to use q=1
as part of the payload data.
CodePudding user response:
It sounds like you're asking "Can I have request parameters as well as a payload in the same HTTP "POST" request?"
Yes: including both ?q=1
in your POST request line and a JSON payload in your message body is perfectly legal.
And no, I don't see any need to revise your API. In fact, for many use cases, configurable request parameters, with a variable payload type, is probably the preferred solution.
For more details, look at RFC 2616: Hypertext Transfer Protocol. See sections 5.1 Request-Line and 9.5 POST.