DEV Community

Cover image for Hypertext Transfer Protocol (HTTP) and Methods.
chryzcode
chryzcode

Posted on

Hypertext Transfer Protocol (HTTP) and Methods.

Hey πŸ‘‹,

Today, you'll be learning more about Hypertext Transfer Protocol (HTTP) and available methods.

Firstly,

- What is HTTP?

The full meaning of (HTTP) is Hypertext Transfer Protocol it is an application-layer protocol for transmitting hypermedia documents, such as HTML.

More about HTTP

It was designed for communication between web browsers and web servers, but it can also be used for other purposes.

HTTP follows a classical client-server model, with a client opening a connection to make a request, then waiting until it receives a response.

HTTP is a stateless protocol, meaning that the server does not keep any data (state) between two requests.

Hypertext Transfer Protocol (HTTP) Methods.

You might have come across basic HTTP methods like GET, POST, PUT and DELETE but I'll explicitly explain more about these basic methods and the others.

- GET

This method is used to request data or resources from a source.

The HTTP GET method is used to retrieve (or read) a resource. In case of success (or non-error), GET returns a representation in JSON or an object(s), its HTTP response status code is 200 (OK). In an error case, it most often returns a 404 (NOT FOUND) or 400 (BAD REQUEST).

- POST

This method is used to send data to a server to create a resource.

The POST method is most often utilized to create new resources. The method takes care of associating the new resource with a model or a database table as specified, assigning an ID (new resource URI), etc.

On successful creation, HTTP response code 201 is returned.

CAUTION ⚠️

Making two identical POST requests will most likely result in two resources containing the same information but with different identifiers. A solution to this is making your important data fields unique.

- PUT

This method is used to send data to a server to update or create a resource. Many web developers want to use PUT for creating a resource on the server because it's idempotent. No matter how many times you call the PUT, the state of the resource will not jeopardize. The contrast difference between the PUT and POST is the idempotent feature

Successful use of this method returns a 201 status code (Created).

- PATCH

This method is used to modify a specified part of a resource. The PATCH request only needs to contain the changes to the resource, not the complete resource.

In other words, the body should contain a set of instructions describing how a resource currently residing on the server should be modified to produce a new version.

CAUTION ⚠️

Collisions from multiple PATCH requests may be dangerous, it is possible to corrupt the resource. Here is the safest way to apply a PATCH method(e.g., GET a resource, ensure it was not modified and apply PATCH) such that the request will fail if the resource has been updated since the client last accessed the resource.

- DELETE

This method deletes a specified resource by filters or ID.

On successful deletion, the HTTP response status code is 204 (No Content) returns with no response body.

IMPORTANT

If you DELETE a resource, it is removed. Repeatedly calling DELETE on that resource will often return a 404 (NOT FOUND) status code since it was already removed and, therefore, is no longer findable.

- HEAD

This method is almost identical to the GET method, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.

- OPTIONS

This method describes the communication options for the target resource.

The HTTP OPTIONS method requests permitted communication options for a given URL or server. A client can specify a URL with this method, or an asterisk (*) to refer to the entire server.

Browsers send an HTTP OPTIONS request to find out the supported HTTP methods and other options supported for the target resource before sending the actual request. HTTP OPTIONS requests allow clients to obtain parameters and requirements for specific resources and server capabilities without taking action on the resource or requesting the resource.

The server response can include an Allow header indicating the allowed HTTP methods for this resource or various CORS (Cross-Origin Resource Sharing) headers.

- TRACE

The TRACE method is used to echo the contents of an HTTP request back to the requester which can be used for debugging purposes at the time of development, it performs a message loop-back test along the path to the target resource, providing a useful debugging mechanism.

The final recipient of the request should reflect the message received, back to the client as the message body of a 200 (OK) response with a Content-Type of message/http

- CONNECT

The CONNECT method is used by the client to establish a network connection to a web server over HTTP. This method starts two-way communications with the requested resource. It can be used to open a tunnel.

Thanks for reading through this blog, you can connect with me on;

Bye πŸ‘‹.

Top comments (0)