HTTP Basics

- 2 mins

HTTP Basics

HTTP FLOW:

  1. Client opens a TCP connection. Client may open a new connection, reuse an existing connection, or open several TCP connections to the servers.
  2. Client sends a simple message encapsulated in a frame (HTTP/2)
  3. Client reads the response sent by the server
  4. Close/reuse the connection as needed for further requests.

HTTP Frames

A frame begins with a fixed 9-octet header followed by a variable-length payload. A closer look at the fields:

Length: length of the payload as an unsigned 24-bit integer

Type: 8-bit type of the frame.

Flags: 8-bit field reserved for boolean flags specific to frame type

R: Reserved 1-bit field

Stream Identifier: unsigned 31-bit

Payload: the representation of some resource - consists of metadata in the form of header fields and data in the form of sequences of octets.

HTTP Requests:

Requests consist of: HTTP Method: GET/POST/PUT/DELETE, etc. that defines the operation the client wants to perform

Path: path of resource to fetch - URL of the resource without protocol, domain, or port.

Version: version of the HTTP protocol

Optional headers or body

HTTP Responses:

Version: version of the HTTP protocol

Status code: indicating success/why or why not

Status message: description of status code

HTTP headers

(optional) Body: contains the fetched resource

HTTP requests are usually what you’ll use to communicate with a website or an API! Check out how simple it is to craft requests with the python requests library (and thanks to jsonplaceholder for the test API!):

>>> import requests
>>> resp = requests.get('https://jsonplaceholder.typicode.com/todos/1')
>>> resp = requests.get('https://jsonplaceholder.typicode.com/todos/1')
>>> resp.ok
True
>>> resp.content
'{\n  "userId": 1,\n  "id": 1,\n  "title": "delectus aut autem",\n  "completed": false\n}'
Tony De La nuez

Tony De La nuez

Site Reliability Engineer @ Atlassian. Interested in software development, infrastructure, games.

comments powered by Disqus
rss facebook twitter github youtube mail spotify instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora