HTTP (Protocol)

Definition:

HTTP, or Hypertext Transfer Protocol, is an Internet protocol widely used for webpages and APIs.

It specifies a “request-response” way of communication between a client and a server. For one HTTP communication the client sends a “request” to server about what data it wants, and the server sends back a “response” about the status of this request (successful or there are some problems) along with the requested data (if the status is “success”).

After the request and response the HTTP communication ends. For further communication the client must initiates another HTTP communication for a new request-response pair.

 

The HTTP request involves three segments:

  1. The verb: this is a word that specifies the purpose of this request. For example “GET” means this request wants to get information. “POST” means this request wants to send data to the server.
  2. The URL: (eg: http://www.mysite.com/search?name=john) this specifies which server to connect (www.mysite.com), what task it wants to invoke (/search), and additional parameters for the task (?name=john)
  3. The body: the body includes two parts:
    • headers: this specifies additional information about the connection.
    • body: this stores other data needed to send to the server.

 

The HTTP response involves two segments:

  1. Status code: a number indicating the status of the request. For example “200” means a successful connection. “4xx” (xx are two digit numbers) means there is an error, and the cause of error is from the request (client’s fault).
  2. Response data.

 

Analogy:

  • Vending machines: you send a request by (1) money (2) drink number. The machine responds with (1) drink (2) changes. The machine responds with error if the drink is sold out, and returns your money. The interaction always ends with a pair of request and response.