user1387147
user1387147

Reputation: 955

Http methods differences

What is difference between

HTTPPOST
HTTPDELETE
HTTPPUT
HTTPGET

Normally used post and get method for submit form and i know them very well but want to know with delete and put method when and why they can be used to improve programming skills

Upvotes: 0

Views: 104

Answers (2)

Lee Kowalkowski
Lee Kowalkowski

Reputation: 11751

In a nutshell:

  • GET = fetch a resource.
  • POST = update a resource.
  • DELETE = delete a resource.
  • PUT = create/replace a resource.

In HTML, only GET and POST are allowed. A typical web-development HTTP server will do nothing unless you have code (or configuration) to specify what you want it to do with the different HTTP methods.

There's nothing stopping you from updating user data in response to a GET request, but it's not advisable. Browsers deal with GET and POST differently, with respect to caching the request (a cached GET will automatically be reissued, but a cached POST will prompt the user to allow it to be resent) and many HTML elements can issue GETs, making them unsafe for updates. There are other HTTP methods too http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol.

Many people who claim to be RESTful will confuse HTTP POST and PUT with SQL UPDATE and INSERT. There isn't a direct correlation, it always depends on context. That is, what POST means depends entirely on the resource that you're interacting with. For example, creating a new entry on a blog could be a POST to the blog itself, or a PUT to a subordinate resource. However, a PUT, by definition, must always contain the entire resource.

Typically, you would not allow a HTTP client to determine the URI of a new resource, so a POST to /blog would be safer than a PUT to /blog/article-uri although HTTP does cater for appropriate responses should the server be unable to honour the intended URI. (HTTP is just a specification, you have to write the code to support it, or find a framework)

But as you can always achieve a PUT or DELETE use-case by POSTING to a parent resource responsible for its subordinates (i.e. POSTing a message to /mailbox instead of PUTting it at /mailbox/message-id), it isn't essential to expose PUT or DELETE methods publicly.

You can improve your programming skills by adopting REST principles to improve the visibility of the interactions within a system, it may be simpler to contextualise your interactions in terms of REST by having a uniform interface, for example.

REST is not HTTP though: http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm.

Upvotes: 1

Jon
Jon

Reputation: 437554

What the different methods do depends entirely on how the remote web server chooses to interpret them. There is no fixed meaning. A server does not care if it sees GET or POST; rather, the code that ends up being executed to service the request does (and can decide to do anything, since it's code).

The HTTP protocol gives an official guideline for what kind of action each verb is supposed to trigger, which is:

  • GET: retrieve a resource
  • PUT: replace a resource with another, or create it if it does not exist
  • DELETE: remove a resource if it exists
  • POST: might do anything; typically used to "add" to a resource

However this mapping is ultimately governed by application code and is typically not respected by web applications (e.g. you will see logical deletions being enacted with POST instead of DELETE).

The situation is better when talking about REST architectures over HTTP.

Upvotes: 1

Related Questions