Example B-2 shows the request message sent from a Netscape browser applying the GET
method to the grapes.gif resource. The action is to retrieve the image stored in the file grapes.gif.
Example B-2. An example HTTP request message
GET /grapes.gif HTTP/1.0 Accept: image/gif, image/jpeg, image/png, */*; Accept-Charset: iso-8859-1,*,utf-8; Accept-Encoding: gzip; Accept-Language: en; Connection: Keep-Alive; Host: www.webdatabasebook.com; User-Agent = Mozilla/4.51 [en] (WinNT; I);
The first line of the message is the request-line and contains the method name GET
, the request URL /grapes.gif
, and the HTTP version HTTP/1.0
, each separated by a space character. The request-line is followed by a list of header fields. Each field is represented as a name and value pair separated with a colon character, and lines are separated with semicolons.
The header fields are followed by a blank line and then by the optional body of the message. The POST
method request usually contains a body of text, as we discuss in the next section.
GET versus POST
Both the GET
and POST
methods send data to the server, but which method should you use?
The HTTP standard includes the two methods to achieve different goals. The POST
method was intended to create a resource. The contents of the resource would be encoded into the body of the HTTP request. For example, an order <form>
might be processed and a new row in a database created.
The GET
method is used when a request has no side effects such as performing a search, and the POST
method is used when a request has side effects such as adding a new row to a database. A more practical issue is that the GET
method may result in long URLs, and may even exceed some browser and server limits on URL length.
Use the POST
method if any of the following are true:
-
The result of the request has persistent side effects such as adding a new database row.
-
If the data collected on the form is likely to result in a large URL if implemented using the
GET
method. -
The data to be sent is in any encoding other than seven-bit ASCII.
Use the GET
method if all the following are true:
-
If the request is essentially finding a resource, and HTML
<form>
data is to help that search. -
The result of the request has no persistent side effects.
-
If the data collected and the input field names in a HTML
<form>
are less than 1,024 characters in length.