- What is the protocol?
- What is the HTTP?
- netcat example
- Request and response
- URL=location, URN=name, URI=type of resource
- HTTP Request Methods
- HTTP status codes
- HTTP 2.0
- Python requests module
- Exceptions
- Postman (form-data, raw, x-www-form-urlencoded)
- Interceptor
- Resources
What is the protocol?
- Protocol is the agreement on how to send and receive and understand the information.
- Example: in past times, people asked help from each other by switching on the fires on top of mountains.
What is the HTTP?
HTTP (hypertext transfer protocol) — is a communication protocol (application layer) which transports messages over internet between client and server. It is the request-response protocol.
- Includes a set of rules for formatting and processing data.
- Designed for communication between web browsers as client and web servers as a server. HTTP request sent to a URL by client and servers sends back HTTP response.
- HTTP is a network protocol used to deliver resources which could be files on the World Wide Web, whether they are HTML files, image files, query results, scripts, or other file types.
- HTTP is over TCP and IP, SOAP is over HTTP
- TCP is used in applications where reliability is more important, such as file transfer, emails, and web browsing.
- UDP is used in applications where speed is more important such as video conferencing, live streaming, and online gaming.
- HTTP is request/response protocol. Client can make a request using
HTTP methods, server returns a response withHTTP status code. - HTTP request contains → Request method, request body (payload), http header.
- HTTP response contains → Status code, response body (payload), http header.
netcat example
Request and response
- Request consists of:
- Method, URI, HTTP-version
- Headers
- Request message body
- Response consists of:
- Status line (HTTP-version, response code, response code small description = reason phrase)
- Headers
- Response message body
- Message body = 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(text/html, application/json, text/plain, text/css, image/gif). The Content-Length: header gives the number of bytes in the body.
URL=location, URN=name, URI=type of resource
- URL is a specific type of URI that specifies the location of a resource on the internet. Includes schema (http, https), domain name, and path to resource.
- URN = type of URI that specifies the name of a resource, rather than its location. URN is intended to be a permanent and unchanging identifier for a resource, regardless of where the resource is located or how it is accessed.
- In general, a URN is not included in a URL. A URL specifies the location of a resource, while a URN specifies the name of a resource.
- URI is a more general concept that can be used to identify any type of resource in internet. A URI can be used to identify any type of resource, including a URL, a file on the local file system, a resource on a private network, and more. URL is type of URI. URN is also a type of URL.
HTTP Request Methods
- GET: retrieve data, the equivalent to a read in CRUD APIs
- POST: add new data
- PUT: update existing data
- PATCH: update a subset of existing data
- DELETE: remove data
For every client data, we are storing an identifier to find that client data and we will send back that identifier to the client for reference.
- POST = not idempotent = duplicates allowed
- If the client sends data without any identifier, then we will store the data and assign/generate a new identifier.
- If the client again sends the same data without any identifier, then we will store the data and assign/generate a new identifier.
- What if make POST with identifier? 404 Wrong
- NOTE: Duplication is allowed here.
- PUT
- If the client sends data with an identifier, then we will check whether that identifier exists. If the identifier exists, we will update the resource with the data, else we will create a resource with the data and assign/generate a new identifier.
- What if the identifier doesn’t exist? → throw 404 error
- PATCH
- If the client sends data with an identifier, then we will check whether that identifier exists. If the identifier exists, we will update the resource with the data, else we will throw an exception.
Note: On the PUT method, we are not throwing an exception if an identifier is not found. But in the PATCH method, we are throwing an exception if the identifier is not found.
HTTP status codes
- 1xx - Informational (request received / processing)
- 2xx - Success (successfully received, understood and accepted)
- 3xx - Redirect
- 4xx - Client error
- 5xx - Server error
- GET — return 200 (OK)
- POST — return 201 (CREATED)
- PUT — return 200 (OK)
- DELETE, PUT — return 204 (NO CONTENT)
HTTP 2.0
- HTTP 1.1 uses text based format to exchange the information, HTTP 2.0 uses binary format
- HTTP 2.0 is fast, it squeezes the header size
- HTTP 1 loads a single request for every TCP connection, while HTTP2 uses 1 single TCP connections for parallelism and avoids network delay by using multiplexing.
Python requests module
Exceptions
ConnectionError- if refused connection, DNS error
Postman (form-data, raw, x-www-form-urlencoded)
- If you need file uploads,
form-datais your only option here. - Difference in content type.
x-www-form-urlencodedusesapplication/x-www-form-urlencoded, but form-data usesmultipart/form-data. Raw text usesContent-Type: text/plain. Raw JSON usesContent-Type: application/json - If you want to send simple text/ASCII data, then
x-www-form-urlencodedwill work. This is the default. Special chars are also handled. - But if you have to send non-ASCII text or large binary data, the
form-datais for that. - In Python, if you do request with raw text, json, xml → you will get data inside
request.datain binary form - If you request with
form-dataorform-urlencodedyou will get data insideform. - Query string (param in Postman) is located in
request.args - https://dev.to/getd/x-www-form-urlencoded-or-form-data-explained-in-2-mins-5hk6
- https://stackoverflow.com/questions/25385559/rest-api-best-practices-args-in-query-string-vs-in-request-body
Interceptor
An interceptor in HTTP refers to a component that can intercept and process HTTP requests and responses before they are handled by the intended recipient. The interceptor can be used to add, modify, or remove headers from a request or response, or to perform other actions such as logging, authentication, or caching.
They can also be used in reverse proxy servers and API gateways to add functionality such as rate limiting, request validation, and security.
Resources
- Building API in Python https://github.com/Asabeneh/30-Days-Of-Python/blob/master/29_Day_Building_API/29_building_API.md
- Requests doc.
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages
- https://realpython.com/python-requests/#headers
- https://stackoverflow.com/questions/15900338/python-request-post-with-param-data