Let's assume a service offers some funcionality that I can use like this:
GET /service/function?param1=value1¶m2=value2
Is it right to say that I can use it with a POST query?
POST /service/function { param1 : value1, param2 : value2 }
Are these two queries the same? Can I use the second variant in any case or the documentation should explicitly say that I can use both GET and POST queries?
Just to review,
REST
has certain properties that a developer should follow in order to make itRESTful
:What is REST?
According to wikipedia:
What the verbs should do
SO user Daniel Vasallo did a good job of laying out the responsibilities of these methods in the question Understanding REST: Verbs, error codes, and authentication:
So, to answer your question:
If you were writing a plain old RPC API call, they could technically interchangeable as long as the processing server side were no different between both calls. However, in order for the call to be RESTful, calling the endpoint via the
GET
method should have a distinct functionality (which is to get resource(s)) from thePOST
method (which is to create new resources).Side note: there is some debate out there about whether or not
POST
should also be allowed to be used to update resources... though i'm not commenting on that, I'm just telling you some people have an issue with that point.In REST, each HTTP verbs has its place and meaning.
For example,
GET is to get the 'resource(s)' that is pointed to in the URL.
POST is to instructure the backend to 'create' a resource of the 'type' pointed to in the URL. You can supplement the POST operation with parameters or additional data in the body of the POST call.
In you case, since you are interested in 'getting' the info using query, thus it should be a GET operation instead of a POST operation.
This wiki may help to further clarify things.
Hope this help!
It is nice that REST brings meaning to HTTP verbs (as they defined) but I prefer to agree with Scott Peal.
Here is also item from WIKI's extended explanation on POST request:
I could only suggest to REST team to consider more secure use of HTTP protocol to avoid making consumers struggle with non-secure "good practice".
I use POST body for anything non-trivial and line-of-business apps for these reasons:
BTW, I also put the fields to return in my POST body as I may not wish to expose my field names. Security is like an onion; many layers and makes us cry!
Think about it. When your client makes a GET request to an URI X, what it's saying to the server is: "I want a representation of the resource located at X, and this operation shouldn't change anything on the server." A PUT request is saying: "I want you to replace whatever is the resource located at X with the new entity I'm giving you on the body of this request". A DELETE request is saying: "I want you to delete whatever is the resource located at X". A PATCH is saying "I'm giving you this diff, and you should try to apply it to the resource at X and tell me if it succeeds." But a POST is saying: "I'm sending you this data subordinated to the resource at X, and we have a previous agreement on what you should do with it."
If you don't have it documented somewhere that the resource expects a POST and does something with it, it doesn't make sense to send a POST to it expecting it to act like a GET.
REST relies on the standardized behavior of the underlying protocol, and POST is precisely the method used for an action that isn't standardized. The result of a GET, PUT and DELETE requests are clearly defined in the standard, but POST isn't. The result of a POST is subordinated to the server, so if it's not documented that you can use POST to do something, you have to assume that you can't.
You can't use the
API
usingPOST
orGET
if they are not build to call using these methods separetly. Like if your API sayis accessed by using
GET
method. Then you can not call it usingPOST
method if it is not specified asPOST
method by its creator. If you do that you may got405 Method not allowed
status.Generally in
POST
method you need to send the content in body with specified format which is described incontent-type
header for ex.application/json
for json data.And after that the request body is gets deserialized at server end. So you need to pass the serialized data from the client and it is decided by the service developer.
But in general terms
GET
is used when server returns some data to the client and have not any impact on server whereasPOST
is used to create some resource on server. So generally it should not be same.