HTTP Request Methods: A Complete Guide
By: Ayush Verma
Tags: JavaScript, Programming, Coding, Software Development, Frontend Development

PATCH

A PATCH request is one of the lesser-known HTTP methods, but it is similar to POST and PUT. The difference with PATCH is that you only apply partial modifications to the resource .

request is one of the lesser-known HTTP methods, but it is similar to POST and PUT. The difference with is that you . The difference between PATCH and PUT, is that a PATCH request is non-idempotent (like a POST request).

(like a POST request). To expand on partial modification, say you’re API has a /users/{{userid}} endpoint, and a user has a username. With a PATCH request, you may only need to send the updated username in the request body - as opposed to POST and PUT which require the full user entity.

As you can see here, the request is very similar to the PUT request, but the body of the request contains only the property of the resource that needs to be changed

DELETE

The DELETE method is exactly as it sounds: delete the resource at the specified URL . This method is one of the more common in RESTful APIs so it's good to know how it works.

method is exactly as it sounds: . This method is one of the more common in RESTful APIs so it's good to know how it works. If a new user is created with a POST request to /users , and it can be retrieved with a GET request to /users/{{userid}} , then making a DELETE request to /users/{{userid}} will completely remove that user.

HEAD

The HEAD method is almost identical to GET , except without the response body . In other words, if GET /users returns a list of users, then HEAD /users will make the same request but won't get back the list of users.

method is almost identical to , . In other words, if returns a list of users, then will make the same request but won't get back the list of users. HEAD requests are useful for checking what a GET request will return before actually making a GET request. For example, if a URL might produce a large download, a HEAD request could read its Content-Length header to check the filesize without actually downloading the file.

The following example demonstrates sending an HTTP HEAD request to the query about the users:

HEAD /users HTTP 1.1

User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)

Host: medium.com

Accept-Language: en-us

Accept-Encoding: gzip, deflate

Connection: Keep-Alive

The response contains the same as the GET method.

HTTP/1.1 200 OK

Date: Mon, 23 Sept 2021 12:28:53 GMT

Server: Apache/2.2.14 (Win32)

Last-Modified: Wed, 22 Jul 2021 19:15:56 GMT

ETag: "34aa387-d-1568eb00"

Vary: Authorization,Accept

Accept-Ranges: bytes

Content-Length: 88

Content-Type: application/json

Connection: Closed

OPTIONS

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. The OPTIONS request should return data describing what other methods and operations the server supports at the given URL.

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. The OPTIONS request should at the given URL. OPTIONS requests are more loosely defined and used than the others, making them a good candidate to test for fatal API errors . If an API isn’t expecting an OPTIONS request, it’s good to put a test case in place that verifies failing behavior.

. If an API isn’t expecting an OPTIONS request, it’s good to put a test case in place that verifies failing behavior. In CORS, a preflight request is sent with the OPTIONS method so that the server can respond if it is acceptable to send the request.

The following example requests a list of methods supported by a web server running on medium.com:

And the server response:

HTTP/1.1 200 OK

Allow: GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS

Access-Control-Allow-Origin: https://medium.com/

Access-Control-Allow-Methods: GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS

Access-Control-Allow-Headers: Content-Type

TRACE

The HTTP TRACE method is designed for diagnostic purposes. It performs a message loop-back test along the path to the target resource, providing a useful debugging mechanism.

is designed for diagnostic purposes. 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, excluding some fields described below, back to the client as the message body of a 200 ( OK ) response with a Content-Type of message/http . The final recipient is either the origin server or the first server to receive a Max-Forwards value of 0 in the request.

The following example shows the usage of the TRACE method:

TRACE / HTTP/1.1

Host: www. medium.com

User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)

The server response is sent back for the above client request.

HTTP/1.1 200 OK

Date: Mon, 23 Sept 2021 12:28:53 GMT

Server: Apache/2.2.14 (Win32)

Connection: close

Content-Type: message/http

Content-Length: 39 TRACE / HTTP/1.1

Host: www. medium.com

User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)

Conclusion

In this guide, we have looked at the eight main HTTP Request Methods. Each of them implements a different semantic, but some common features are shared by a group of them: e.g. a request method can be safe, idempotent, or cacheable.

Safe methods: GET , HEAD , OPTIONS, TRACE Idempotent methods: GET , PUT , DELETE , HEAD,OPTIONS , TRACE Cacheable methods: GET, HEAD

A response to a POST or PATCH request can also be cached if freshness is indicated and the Content-Location header is set, but this is rarely implemented.

We have done a comparison of GET vs POST and PUT vs POST. With this knowledge, I hope you are able to use the HTTP methods constructively.

As always, thanks for reading. If you liked this post please share it and check out some of my other posts here on Medium.

More content at plainenglish.io