0

I know it a basic question but i was not able to find a answer. My question is why we need POST method in restful when we can even insert data from GET method also. If there any specific functionality which make POST different from GET.

Thanks.

suresh
  • 167
  • 2
  • 12
  • 1
    "we can even insert data from GET method also"... We can't insert any data using GET. GET is only to read the data. – sauumum May 18 '16 at 16:25
  • 2
    1) In POST, the data is in the body and can have theoretically unlimited size, while GET has the parameters in the URL query and there are several URL size limits around - from the browser, from the firewall, from routers, from the servers, etc. 2) POST is defined as changing something, while GET is not supposed to change anything on the server. So browsers will resend GET requests freely when navigating or retrying, but warn the user when a POST was about to be resent to prevent them from buying something twice for example. Also, for the same reason, POST results are not cached. – CherryDT May 18 '16 at 16:25
  • I think you want to know the difference between PUT and POST. If my assumption is correct , please look http://stackoverflow.com/questions/630453/put-vs-post-in-rest . – sauumum May 18 '16 at 16:27
  • @SauravKumarMehta Sorry if i am wrong but we can write business logic inside GET method by which we can insert data. Sorry correct me if i am wrong – suresh May 18 '16 at 16:28
  • @suresh : Please see https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.3 . As per HHTP standard, we should not try to modify anything on server using GET. – sauumum May 18 '16 at 16:30
  • @SauravKumarMehta Thanks that was useful – suresh May 18 '16 at 16:58
  • Note also that several modern browsers will pre-call all possible GETs on the page to speed up loading times, which is not a good thing if those things aren't idempotent (for example, one site *logs you out* from a GET...) – jonrsharpe May 18 '16 at 17:10

2 Answers2

2

It is a good practice to use the standard methods offered by the HTTP protocol to handle the requests for a web service Restful:

  • GET to retrieve data
  • POST to update a record
  • PUT to insert a record
  • DELETE to delete a record

Following this convention is easy for a person to understand a library that he doesn't know.

Just to have an idea if I need to get all companies

GET /companies

To retrieve a particular company identifiable by 1

GET /companies/1

To create a new company:

PUT /companies

TO update the company identifiable by 1

POST /companies/1

To delete the company identified by 1

DELETE /companies/1

And extending this concept, to retrieve all the dependents of the company 1

GET /companies/1/dependents

To retrieve all the invoices of a company

GET /companies/1/invoices

and so on.

As you can see if you know what you like to do is easy to recreate all the urls to get, modify, create, delete data. It is not necessary to follow this convention, but it is a good idea specially if you are creating a web service usable from outside your company where it is important to define a standard for all.


Additionally, GET methods can be cached, and it is easy for existing infrastructure (proxies, firewalls) to do that.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
1

They are different methods and have different purpose and specification.

Some other notes on GET requests:

  • GET requests can be cached
  • GET requests remain in the browser history
  • GET requests can be bookmarked
  • GET requests should never be used when dealing with sensitive data
  • GET requests have length restrictions
  • GET requests should be used only to retrieve data

Some other notes on POST requests:

  • POST requests are never cached
  • POST requests do not remain in the browser history
  • POST requests cannot be bookmarked
  • POST requests have no restrictions on data length

They should be used appropriately. For more info on use and specification, look here.

Pang
  • 9,564
  • 146
  • 81
  • 122