suresh
suresh

Reputation: 177

Why we need GET and POST method in RestFul when GET can behave as POST also

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.

Upvotes: 0

Views: 129

Answers (2)

Abylay Sabirgaliyev
Abylay Sabirgaliyev

Reputation: 726

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.

Upvotes: 1

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

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.

Upvotes: 2

Related Questions