DEV Community

loading...

Introduction to HTTP in a Nutshell

Mani Sai Prasad
Hey there I am a Student Developer I work with various Computer Science TEch 👨🏽‍💻 I am also a Microsoft Student Partner, Mentor at Den X, Dean School of AI, Traveller, Speaker, and a Data Scientist
・3 min read

Hypertext Transfer Protocol (HTTP) is a protocol that provides a standardized way for computers to communicate with each other. It has been the foundation for data communication over the internet since 1990 and is integral to understanding how client-server communication functions.

Features of HTTP

Connectionless
When a request is sent, the client opens the connection; once a response is received, the client closes the connection. The client and server only maintain a connection during the response and request. Future responses are made on a new connection.

Stateless
There is no dependency between successive requests. Each request is an individual request

Not Sessionless
Utilizing headers and cookies, sessions can be created to allow each HTTP request to share the same context.

Media Independent:
Any type of data can be sent over HTTP as long as both the client and server know how to handle the data format. In our case, we'll use JSON.

Elements:

Universal Resource Identifiers (URIs):
An example URI is https://github.com/manisaiprasad?tab=repositories It has certain components:

Scheme:
specifies the protocol used to access the resource, HTTP or HTTPS. In our example HTTPS.

Host:
specifies the host that holds the resources. In our example github.com.

Path:
specifies the specific resource being requested. In our example, /manisaiprasad.

Query:
an optional component, the query string provides information the resource can use for some purpose such as a search parameter. In our example, tab=repositories.

HTTP Requests

HTTP requests are sent from the client to the server to initiate some operation. In addition to the URL, HTTP requests have other elements to specify the requested resource.

Elements of HTTP Requests:

Method: Defines the operation to be performed

Path: The URL of the resource to be fetched, excluding the scheme and host

HTTP Version

Headers:
optional information, success as Accept-Language

Body: optional information, usually for methods such as POST and PATCH, which contain the resource being sent to the server

Request Methods

Different request methods indicate different operations to be performed. It's essential to attend to this to correctly format your requests and properly structure an API.

GET: ONLY retrieves information for the requested resource of the given URI

POST: Send data to the server to create a new resource.

PUT: Replaces all of the representation of the target resource with the requested data

PATCH: Partially modifies the representation of the target resource with the requested data

DELETE: Removes all of the representation of the resource specified by the URI

OPTIONS: Sends the communication options for the requested resource

HTTP Responses

After the request has been received by the server and processed, the server returns an HTTP response message to the client. The response informs the client of the outcome of the requested operation.

Elements of HTTP Responses:

Status Code & Status Message

HTTP Version

Headers: similar to the request headers, provides information about the response and resource representation. Some common headers include:

Date

Content-Type: the media type of the body of the request

Body: optional data containing the requested resource

Status Codes:

As an API developer, it's important to send the correct status code. As a developer using an API, the status codes—particularly the error codes—are important for understanding what caused an error and how to proceed.

Codes fall into five categories:

1xx Informational (100's)
2xx Success (200's)
3xx Redirection (300's)
4xx Client Error (400's)
5xx Server Error (500's)

Common Codes:

200: OK
201: Created
304: Not Modified
400: Bad Request
401: Unauthorized
404: Not Found
405: Method Not Allowed
500: Internal Server Error

Discussion (0)