Thursday, August 6, 2015

HTTP Methods

HTTP (Hyper Text Transfer Protocol) enables the communication between client and server. It works as a request response protocol. An HTTP client opens a connection and sends a request message to an HTTP server; the server then returns a response message, usually containing the resource that was requested. After delivering the response, the server closes the connection.

Structure of HTTP transactions

Both response and request messages consist of :
  • An initial line
  • Zero or more header lines
  • Blank line
  • An optional message body  

Initial request line

A request line has three sections, separated by space : a method name, the local path of the requested resource, and the version of the HTTP being used. 

 eg: GET /path/to/file/index.html HTTP/1.0

*The HTTP version always takes the form "HTTP/x.x", uppercase.

Initial response line

The initial line is different for the response than for the request. But it also has three parts separated by space : the HTTP version, a response status code, phase that describes the status 

eg: HTTP/1.1 405 Method Not Allowed

The status code is a three-digit integer, and the first digit identifies the general category of response:
  • 1xx indicates an informational message only
  • 2xx indicates success of some kind
  • 3xx redirects the client to another URL
  • 4xx indicates an error on the client's part
  • 5xx indicates an error on the server's part 
Header Lines
Header lines provide information about the request or response, or about the object sent in the message body.

The Message Body
An HTTP message may have a body of data sent after the header lines. In a response, this is where the requested resource is returned to the client (the most common use of the message body), or perhaps explanatory text if there's an error. In a request, this is where user-entered data or uploaded files are sent to the server.
If an HTTP message includes a body, there are usually header lines in the message that describe the body. In particular,

  • The Content-Type: header gives the MIME-type of the data in the body, such as text/html or image/gif.
  • The Content-length:  header gives the number of bytes in the body.