Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out
Get this document free

GET VS POST METHODS

VIEWS: 765 PAGES: 5

What is difference b/w get and post methods

More Info
									GET vs POST - Links vs Forms
(Saturday, 13 January 2007) - Written by Administrator - Last Updated (Saturday, 13 January 2007)

There are two common HTTP Request methods used by web developers. The first and most common is the HTTP GET Method. This is a simple request for a web page by its URL, and is the basis of the Hyperlink. The other is the HTTP POST Method and is the basis of HTML Forms.

The HTTP GET method:

Example of Link: www.example.com

When a user clicks on this link, their web browser will retrieve the URL specified. It will do this via a HTTP GET Request. The GET Request is a plain text message, that is sent to the server the user wants to get a page from.

The HTTP GET Request (message) looks something like this:

GET /index.html HTTP/1.1 HOST: www.example.com

Very simple huh? Yes, very. Structure of a HTTP GET Request

- The GET, lets the server know we want to use the GET HTTP Method. - /index.html is the page we want to retrieve. - HTTP/1.1 is the protocol and protocol version we want to use. - HOST, is the domain we want to retrieve the page from. Once the Server receives the request, it will send back the HTTP Response, which will contain the resource requested, which is usually an HTML page.

When I say page, what I really should be saying is URI, or resource. As nowdays, most sites are dynamic. We do not just retrieve pages, we can retrieve different resources, such as audio, video, a dynamic page, XMLEXtensible Markup Language - A plain text format for representing and storing data that is human readable and while being very extensible and flexible. data, JSONJavaScript Object Notation - A lightweight, plain text notation of JavaScript Objects that allows JavaScript Objects to be sent from Server to Client and vise versa via HTTP. Used mostly in AJAX applications. strings etc.

http://fijiwebdesign.com - Fiji Web Design

Powered by Mambo

Generated: 6 August, 2007, 08:39

The HTTP POST method: The second most common HTTP Request method used by web developers is the HTTP POST Method. On a browser, the most common way to create a HTTP POST is via the submission of a FORM with the method set to post (a form post).

A HTTP POST FORM looks something like this:

User: Password:

This simple HTML Form will send a users name and password to the page "login.php". When a user clicks on the Form Submit Button (labeled send), the browser will make a HTTP POST Request to the server, sending it the FORM data.

If we fill in this Form and submit it from www.example.com then the HTTP POST Request will looks something like this:

POST /login.php HTTP/1.1 HOST: www.example.com Content-Type: application/x-www-form-urlencoded user=Joe&password=ilovemary&send=send

Pretty simple aye? Aye. We can see the the form was filled in with the following data:

user: Joe password: ilovemary send: send

"send" is set to "send" because forms will also submit the name and corresponding value of the submit button that was clicked. See: Multiple Form Submit Buttons for an explanation.

The Content-Type is a HTTP Request Header that tells the server what type of data we are sending. Since it is set to: application/x-www-form-urlencoded, the server will know that the data is formatted as url encoded data, sent via an HTML form.

GET vs POST
http://fijiwebdesign.com - Fiji Web Design Powered by Mambo Generated: 6 August, 2007, 08:39

Unfortunately, most web developers do not understand the difference between GET and POST requests and thus use them interchangeably or based on the following reasons:

Why most web developers use a Hyperlink:

- A Hyperlink requires less HTML markup. - A Hyperlink does not need a form processor, and thus needs no server side scripting. - A Hyperlink is followed by web spiders.

Why most web developer use a Form:

- An HTML form hides the data to be sent to the server. (no! No! No!) - A Form allows the sending of larger amounts of data then a link - A Form can send File uploads, a link cannot.

The good part is that most of these reasons are OK.

The reasons that are false are:

- An HTML form hides the data to be sent to the server – This is entirely false. Like we saw in the raw HTTP Request created by a HTTP POST Request, you can view what the user sent just like you would with a Hyperlink. - A Hyperlink does not need a form processor, and thus needs no server side scripting – Most websites today are dynamic and require server side processing for most the HTTP requests they receive.

When should a web developer use GET and when should they use POST? The HTTP/1.1 RFCHypertext Transfer Protocol version 1.1 Request For Comment defines two categories that a HTTP Request method can fall into. These are: "Safe methods" and "Idempotent method".

Safe Methods A GET request is a Safe methodSafe in this case means "safe to cache" or "safe for the user to click without changing stuff on the server. Do not confuse with safe as opposed to unsafe. It has nothing to do with security, that is implemented by your server, and server script., which is a request that does not change a resource on the server. Thus a link, should identify a unique resource on the server that is relatively static and thus can be cached by HTTP or the browser. Examples of this are: web pages, media such as video and audio files, a database result that needs to be cached now and then.
http://fijiwebdesign.com - Fiji Web Design Powered by Mambo Generated: 6 August, 2007, 08:39

Idempotent Methods A POST request is and Idempotent method, which is a request that invokes a change to the resource on the server. Examples of this: Saving data to a database, uploading a file, sending an email, logging into a private section of the website etc.

Thus we should have the following reasons for choosing between GET or POST, or between a Hyperlink or FORM.

When to use a Hyperlink (HTTP GET Request):

- When you are identifying a unique resource on the server. (Hence the URL). - When you have a dynamic resource, that would benefit from the HTTP protocols cache features.

When to use a Form (HTTP POST Request):

- When you want to upload files to the server. - If the data you are sending is unique and should be sent only once. - When you want to manipulate/edit a resource on the server - such as a database record.

Fortunately, the restrictions of the two different implementations of the HTTP Request methods on the browser has made developers use the right method most the time. However, with the advent of XMLHTTPRequest and other AJAX technologies in browsers, GET and POST requests have been interchanged without regard their initial specifications.

What we, web developers should really be doing is making sure we follow the correct reasons for choose a HTTP Request Method, and not just choose a single reason. With AJAX, most developers will choose the POST request just because it is unlikely it will be cached, even if it requests a resource of the server that is quite static. The true way to deal with this is to make a GET request, then on the server side, set the correct HTTP Response Headers that will control caching of the data.

Benefits for you as a Web Developer. If you use GET and POST where they should be used you'll find:

- Resources on your server will be identified better by Search Engines. - Your data will be cached appropriately, and there will be less load on your server. - Your users will view faster pages, due to caching. Great for those on dial-up.
http://fijiwebdesign.com - Fiji Web Design Powered by Mambo Generated: 6 August, 2007, 08:39

- Usability of your pages will increase, as users understand that buttons are for stuff that change stuff, and links are for going to another page. - There will be less conflict between the changes of your web identified resources such as database data, file uploads etc.

A good disucssion on this can be found here: http://www.w3.org/2001/tag/doc/whenToUseGet.html

Ref: HTTP Specs - http://www.ietf.org/rfc/rfc2616.txt Discussion - http://www.daniweb.com/techtalkforums/post300822.html

http://fijiwebdesign.com - Fiji Web Design

Powered by Mambo

Generated: 6 August, 2007, 08:39


								
To top