Friday, October 16, 2009

Http request and Http Response

HTTP Request and Response

The HTTP Request contains an HTTP method which tells the server the type of request that is being made. The HTTP adds a header to its request and response.

GET and POST
The common HTTP methods are GET and POST.

The GET method is used to get a resource from the server. For eg: when the user clicks a link. The GET method can send limited quantity (depending on server) of data to the server. The data that is send via GET method is appended to the URL and is exposed to everyone. This data (called parameter) is separated by "?" in the URL. The GET request can be bookmarked. The GET is used for getting things and not making any changes to the server

The POST method can request a resource from the server and also sends the form data (called payload) to the server.For eg: when the user enters some data and submits the form by clicking the submit button. The parameters are put in the payload. POST request can't be bookmarked. The POST is used for sending data to be processed and this data is used to change something on the server. (an update)


Sample HTTP GET request:
-----------------------------------------------------------------
GET /fruits/grapes.jsp?color=green&type=seedless HTTP/1.1
Host: www.freshfruits.com
User-Agent: Mozilla
Accept: text/html
Accept-Language:en-us
Keep-Alive:300
Connection:keep-alive
-----------------------------------------------------------------
Sample HTTP POST request
------------------------------------------------------------------
POST /fruits/grapes.jsp HTTP/1.1
Host: www.freshfruits.com
User-Agent: Mozilla
Accept: text/html
Accept-Language:en-us
Keep-Alive:300
Connection:keep-alive
color=green&type=seedless --> This is the message body or Payload <--
----------------------------------------------------------------------
The other HTTP methods are HEAD, TRACE, PUT, DELETE. OPTIONS and CONNECT.
The HTTP Response contains the requested resource in the form of HTML. The response has a header and body. The requested resource is put in the body of the response.The header tells the browser the protocol that is used, whether the request is success or not, the type of the content in the body(MIME type).

Sample HTTP Response:
-----------------------------------------------------------
HTTP/1.1 200 OK
set-cookie: JSESSIONID=0B4587RT
content-type:text/html
content-length:242
date: wed, 23 Sep 2009 02:45:09 GMT
Server: Appache
Connection: close
< html >
...........the requested resource
..........
< /html >
-------------------------------------------------------------------

Followers