Post Vs Post, Get Vs Get

When do you use POST and when do you use GET?

Use POST for destructive actions such as creation (I'm aware of the irony), editing, and deletion, because you can't hit a POST action in the address bar of your browser. Use GET when it's safe to allow a person to call an action. So a URL like:

http://myblog.org/admin/posts/delete/357

Should bring you to a confirmation page, rather than simply deleting the item. It's far easier to avoid accidents this way.

POST is also more secure than GET, because you aren't sticking information into a URL. And so using GET as the method for an HTML form that collects a password or other sensitive information is not the best idea.

One final note: POST can transmit a larger amount of information than GET. 'POST' has no size restrictions for transmitted data, whilst 'GET' is limited to 2048 characters.

What is the difference between POST and GET?

GET and POST are two different types of HTTP requests.

According to Wikipedia:

GET requests a representation of the specified resource. Note that GET should not be used for operations that cause side-effects, such as using it for taking actions in web applications. One reason for this is that GET may be used arbitrarily by robots or crawlers, which should not need to consider the side effects that a request should cause.

and

POST submits data to be processed (e.g., from an HTML form) to the identified resource. The data is included in the body of the request. This may result in the creation of a new resource or the updates of existing resources or both.

So essentially GET is used to retrieve remote data, and POST is used to insert/update remote data.



HTTP/1.1 specification (RFC 2616) section 9 Method Definitions contains more information on GET and POST as well as the other HTTP methods, if you are interested.

In addition to explaining the intended uses of each method, the spec also provides at least one practical reason for why GET should only be used to retrieve data:

Authors of services which use the HTTP protocol SHOULD NOT use GET based forms for the submission of sensitive data, because this will cause this data to be encoded in the Request-URI. Many existing servers, proxies, and user agents will log the request URI in some place where it might be visible to third parties. Servers can use POST-based form submission instead



Finally, an important consideration when using GET for AJAX requests is that some browsers - IE in particular - will cache the results of a GET request. So if you, for example, poll using the same GET request you will always get back the same results, even if the data you are querying is being updated server-side. One way to alleviate this problem is to make the URL unique for each request by appending a timestamp.

When should I use GET or POST method? What's the difference between them?

It's not a matter of security. The HTTP protocol defines GET-type requests as being idempotent, while POSTs may have side effects. In plain English, that means that GET is used for viewing something, without changing it, while POST is used for changing something. For example, a search page should use GET, while a form that changes your password should use POST.

Also, note that PHP confuses the concepts a bit. A POST request gets input from the query string and through the request body. A GET request just gets input from the query string. So a POST request is a superset of a GET request; you can use $_GET in a POST request, and it may even make sense to have parameters with the same name in $_POST and $_GET that mean different things.

For example, let's say you have a form for editing an article. The article-id may be in the query string (and, so, available through $_GET['id']), but let's say that you want to change the article-id. The new id may then be present in the request body ($_POST['id']). OK, perhaps that's not the best example, but I hope it illustrates the difference between the two.

Difference between GET and POST methods?

GET:

  • Parameters remain in browser history because they are part of the URL
  • Can be bookmarked.
  • GET method should not be used when sending passwords or other sensitive information.
  • 7607 character maximum size.
  • Url example: page2.php?category=sport

POST:

  • Parameters are not saved in browser history.
  • Can not be bookmarked.
  • POST method used when sending passwords or other sensitive information.
  • 8 Mb max size for the POST method.
  • Url example: page2.php

How should I choose between GET and POST methods in HTML forms?

To choose between them I use this simple rule:

GET for reads. (reading data and displaying it)

POST for anything that writes (i.e updating a database table, deleting an entry, etc.)

The other consideration is that GET is subjected to the maximum URI length and of course can't handle file uploads.

This page has a good summary.

POST vs post, GET vs get

W3C has tended towards lowercase for attribute names and values for a while.

For example section 4.11 of the xhtml 1.0 standard in 2002:

4.11. Attributes with pre-defined value sets


HTML 4 and XHTML both have some
attributes that have pre-defined and
limited sets of values (e.g. the type
attribute of the input element). In
SGML and XML, these are called
enumerated attributes. Under HTML 4,
the interpretation of these values was
case-insensitive, so a value of TEXT
was equivalent to a value of text.
Under XML, the interpretation of these
values is case-sensitive, and in XHTML
1 all of these values are defined in
lower-case.

REST API using POST instead of GET

You can't use the API using POST or GET if they are not build to call using these methods separetly. Like if your API say

/service/function?param1=value1¶m2=value2

is accessed by using GET method. Then you can not call it using POST method if it is not specified as POST method by its creator. If you do that you may got 405 Method not allowed status.

Generally in POST method you need to send the content in body with specified format which is described in content-type header for ex. application/json for json data.

And after that the request body 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 whereas POST is used to create some resource on server. So generally it should not be same.

Why is the GET method faster than POST in HTTP?

It's not much about speed. There are plenty of cases where POST is more applicable. For example, search engines will index GET URLs and browsers can bookmark them and make them show up in history. As a result, if you take actions like modifying a DB based on a GET request, it might be harmful as some bots might also traverse the URL.

The other case can be security issue. If you post credentials using GET, it'll get listed in browser history and server log files.

GET vs POST in Ajax

GET is designed for getting data from the server. POST (and lesser-known friends PUT and DELETE) are designed for modifying data on the server.

A GET request should never cause data to be removed from an application. If you have a link you can click on with a GET to remove data, then Google spidering your site could click on all your "Delete" links.

The canonical answer can be found here, which quotes the HTML 2.0 spec:

If the processing of a form is idempotent (i.e. it has no lasting
observable effect on the state of the
world), then the form method should be
GET. Many database searches have no
visible side-effects and make ideal
applications of query forms.

If the service associated with the processing of a form has side effects
(for example, modification of a
database or subscription to a
service), the method should be POST.

In your AJAX call, you need to use whatever method your server supports. You should always design your server so that operations that modify data are called by POST/PUT/DELETE. Other comments have links to REST, which generally maps C/R/U/D to "POST or PUT"(Create)/GET(Read)/PUT(Update)/DELETE(Delete).

jquery $.post() vs $.get()

If you just want to retrieve the contents from an html document, use $.load() instead.

You can even retrieve partial information from that document by providing an additional selector:

$('#result').load('ajax/test.html');
$('#result').load('ajax/test.html #justThisContainerPlease');

see http://api.jquery.com/load/


To answer your question more generally, its no big difference whether you're using a POST or a GET request to a server, it depends on the amount of data you need to send. Typically, a GET request is limited to 2083 (because IE limits the query-string). So if you have a lot of data to send, you should use a POST request.

Technically, a GET request should be slightly faster. Because internally only one packet is sent instead of at least two (one for the header and one for the transmission body). But that really is high performance optimizing.



Related Topics



Leave a reply



Submit