In one of the questions, you can check PUT vs POST in RESTful services, but now in this article, I have provided difference between get vs post considering rest api, which are used to retrieve or save data in database using REST API.
What is GET Method?
The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI. If the Request-URI refers to a data-producing process, it is the produced data which shall be returned as the entity in the response and not the source text of the process, unless that text happens to be the output of the process.
A GET request retrieves data from a web server by specifying parameters in the URL portion of the request.
GET is the main method used for static document retrieval.
GET API methods should be idempotent, which means that making multiple identical requests must produce the same result every time until another API methods (POST or PUT) has changed the state of the resource on the server.
Example of URL requests for GET:
HTTP GET http://www.yourdomain.com/users HTTP GET http://www.yourdomain.com/users?pageSize=10&page=5 HTTP GET http://www.yourdomain.com/users/11 HTTP GET http://www.yourdomain.com/users/123/address
What is POST Method?
The POST request method is used to send data to the server to create a new resource.
When we submit POST request to server, for creating any new resource or data, then it should return 201 status, to let client side(method caller) know that request has been submitted and processed successfully.
We can consider a below example, in which we will be sending data as form using the default application/x-www-form-urlencoded content type:
Many times, the action performed by the POST method might not result in a resource that can be identified by a URI.
In this case, either HTTP response code 200 (OK) or 204 (No Content) is the appropriate response status.
Responses of POST method are not cacheable unless cache expiration header is included.
Difference Between Get and Post
Let's take a look at major difference between GET and POST using table below
|GET Request is used whenever we are retrieving some data.||POST is used when we are saving or updating data in database.|
|GET Request is cacheable||Request is hardly cacheable|
|GET Request has characters limit of 255||There is no character limit, you can send any limit of data in body|
|This method only supports string data type.||POST supports all forms of datatype like numeric, string, binary etc.|
|Values passed to server is visible||Values passed to is not visible and passed inside body|
|Sensitive data should never be passed using the GET method, since it can be cached.||Can pass sensitive data over HTTPS (Secured) network, like passwords etc are sent using POST method.|
Which HTTP Method (GET or POST) to use when?
Now, that you know the difference between HTTP GET and POST methods of Rest API, and you are still in a situtation, like which method to use when, then let me clear it in simple words.
Use GET when you want data from server or third party API, as it returns data from server (API)
Use POST to submit data to server using API, so data can be processed on server and saved into database using API code, so you can save new data or update existing data in database.
GET is safe method, since we are not updating or anything on server.
You may also like to read: