What is http request/response?

GOAL

To understand mechanism and meaning of http request and response message.

What is HTTP?

HTTP stands for Hypertext Transfer Protocol that is a protocol of transfer of hypertext written in HTML or XML, and other data.

HTTP is request-response type data communication protocol. The client sends request to the server and the server sends to the response.

HTTP Request

These are example of HTTP request.

GET / HTTP/1.1
Accept: image/image01.png, */*
Accept-Language: ja
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (Compatible; MSIE 6.0; Windows NT 5.1;)
Host: www.example.com
Connection: Keep-Alive
GET /index.html HTTP/1.1 
Host: localhost:8080
Connection: keep-alive
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) 
Referer: http://localhost:8080/
Accept-Encoding: gzip, deflate
Accept-Language: ja,en-US

HTTP Response

These are example of HTTP request.

HTTP/1.1 200 OK
Date: Sat, 09 Oct 2010 14:28:02 GMT
Server: Apache
Last-Modified: Tue, 01 Dec 2009 20:18:22 GMT
ETag: "51142bc1-7449-479b075b2891b"
Accept-Ranges: bytes
Content-Length: 29769
Content-Type: text/html

<!DOCTYPE html... 

Constitution of HTTP request

HTTP request consists of request line, HTTP header and body.

Request line

Request line is the first line of the HTTP request.

 GET /index.html HTTP/1.1 

Request line consists of request method, request URI and HTTP version.

Request Methods

  • GET
  • HEAD
  • POST
  • PUT
  • DELETE
  • CONNECT
  • OPTIONS
  • TRACE
  • PATCH

Refer “HTTP request methods” for detail of each method.

Constitution of HTTP response

HTTP response consists of Status line, HTTP header and body.

Status line

Status line is the first line of the HTTP response.

HTTP/1.1 200 OK

Status line consists of HTTP version, Status Code and Reason Phrase.

Status Code

Status code has 5 class

  • Informational responses (100–199)
  • Successful responses (200–299)
  • Redirects (300–399)
  • Client errors (400–499)
  • Server errors (500–599)

Refer “HTTP response status codes” for detail of status code.

HTTP header

HTTP header contains fields and their values as a format “field: value”.

GET / HTTP/1.1
Accept: image/image01.png, */*
Accept-Language: ja
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (Compatible; MSIE 6.0; Windows NT 5.1;)
Host: www.example.com
Connection: Keep-Alive

There are 4 types of HTTP header, General headers, Request headers, Response headers, Entity headers.

General headers

Examples of General headers

  • Connection: Keep-Alive or Close
    • whether or not the network connection stays open after the current transaction finishes
  • Date: <day-name>,<day>,<month>, <year>, <hour>:<minute>:<second> GMT
    •  the date and time when the message was originated
  • Cache-Control: public, no-cache, no-store, proxy-revalidate and so on
    • instructions for caching in both requests and responses

Request headers

Examples of Request headers

  • Accept-***:
    • items that the client is able to accept
    • Accept-Charset: utf-8, iso-8859-1 and so on
    • Accept-Language: en-US, ja and so on
    • Accept-Encoding: gzip, deflate, br and so on
  • If-***
    • conditional request
    • If-Match: ETag
    • If-Modified-Since: <day-name>,<day>,<month>, <year>, <hour>:<minute>:<second> GMT
  • User-Agent: <product>/<product-version> <system and platform>
    • the application, operating system, vendor and version of the requesting user agent.
  • Referer: <url>
    • the address of the previous web page from which a link to the currently requested page was followed

Response headers

Examples of Request headers

  • Age: <delta-seconds>
    • the time in seconds the object has been in a proxy cache
  • Location: <url>
    • the URL to redirect a page to

Entity headers

Examples of Entity headers

  • Content-Length: <length>
    • the size of the entity-body, in bytes, sent to the recipient
  • Allow: <http-methods> such as GET, POST and HEAD
    • the set of methods supported by a resource

What is body in HTTP request/response?

In HTTP request the body contains parameters. If the method doesn’t need any parameter to pass, body is blank. This is example of body in HTTP request. (name and age are parameters.)

name=taro&age=18

In HTTP response the body is content requested such as hypertext written in HTML. This is example of body in HTTP response.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>The HTML5 Herald</title>...