What Are HTTP Methods and How Can You Use Them?

Every HTTP request uses a method to describe its essential purpose. Each method has specific uses for tasks ranging from fetching a web page to deleting a resource. GET and POST are the two most familiar, but there are seven others.

Learn about this concept and what each of the nine possible HTTP methods is for.

4

What Is an HTTP Method?

Each HTTP request you make includes an action and a target for that action, much like a verb and a noun that acts as an object.

The URL identifies a unique resource, acting as the target of the request. Meanwhile, the verb—what action to carry out on that URL—is represented by a method.

A network router with several coax cables plugged into it.

The first line of an HTTP request provides the most significant information including the HTTP version, the resource URL, and the request method:

What Are the 9 HTTP Methods?

The first four methods are equivalents of the four actions from the CRUD model: Create, Read, Update, and Delete. The respective HTTP methods arePOST,GET,PUT, andDELETE.

The most common, default request you’ll send when browsing the web and clicking links or requesting data via an API. The server should return the resource in its response body.

The Chrome developer tools panel showing 3 requests with different methods: POST, GET, and PATCH

Use POST to create new resources, particularly when those resources have an identifiable parent resource. For example, if you have a /clients resource, you should create a new client by POSTing required data to /clients.

Update or replace an existing resource. This is similar to POST, but the client is responsible for choosing a unique identifier for the resource. To create a resource using PUT, the resource URL should include an identifier, for example,/clients/007.

An example GET request using curl.

Use this method to delete the resource you specify in the URL. Like all HTTP methods, support is down to the server. It may let anyone delete a resource, although that would clearly be risky. A server that supports DELETE will usually requireauthentication and authorization.

Two other methods provide slight variations on the above:PATCHandHEAD. You may find them useful when using certain APIs or if you’re building your own.

An example POST request using curl.

The PATCH method updates an existing resource, with only partial data. In other words, you don’t need to supply the full representation of the new resource, just the fields that you need to update.

A HEAD request is for when you want information about a resource, but not the resource itself. The response will not include a body, but it will contain a set of useful HTTP headers. you could find out the total size of a file before you download it, via the Content-Length response header.

The remaining methods—OPTIONS,CONNECT, andTRACE—are more obscure. They deal with metadata, networking, and troubleshooting. You may find them useful in certain types of programming, but you probably won’t use them every day.

A server should respond to this method by telling you which HTTP methods the resource actually supports. This can be useful for discovery.

Certain types of networking software may use this method to establish a tunnel between two computers. This is often used to initiate an HTTPS connection via a proxy.

This method is useful for troubleshooting. On receiving it, the server should send back the request it received in the response body. The method provides a mechanism for checking whether intermediate machines have changed any details of the request.

GET and HEAD are the only two methods that every general web server supports. A particular server may or may not support other methods, so you should check first.

When Might You Use These Methods?

Much of the time, HTTP methods will be transparent to you. However, you’ll need to consider them if you add a form to a web page or use an API.

HTTP Methods in HTML

Theformelement’smethodattribute lets you set the HTTP method to use when the form submits. HTML only supports thepostandgetvalues for this attribute—plus an obscure dialog alternative.

you may use features likeChrome’s Developer Toolsto viewNetworktraffic and confirm the method your browser sends for a particular request. Note that most web forms will send a POST since they typically update data on the server. Many search forms, however, use GET since they simply fetch data without changing it.

Because POST data isn’t part of the URL, it is more private than if you send it via a GET. The data still exists in the body of the request where, in theory, an attacker may intercept it. But sending data via HTTPS should mitigate this problem.

You should never implement a user login with GET requests. Even though login may seem more like a read operation than a create or update, you still need to use POST to secure the data.

Since HTML forms only support GET and POST, you can’t directly create a form that, for example, deletes a resource on the server. A common workaround, which respects REST principles on the server, is to use a placeholder variable and pretend that was the original HTTP method. For example:

Your server-side code can check for the presence of this ‘proxy method’ variable and treat it as if it were the real method used. For example, in PHP:

Sending HTTP Requests Programmatically

The Web Fetch API provides a fetch() JavaScript function that you can use to send an HTTP request. It supports the full range of methods, not just the GET and POST that HTML supports.

The fetch function will send a GET request by default. To use a different method, specify its name as the value for amethodproperty in an object that you send as the second argument.

You can also send HTTP requests from many server-side languages, including PHP. In that language, the curl library is a common way of sending such requests.

The curl library has a CURLOPT_CUSTOMREQUEST option you can set to specify a method

Via the Command Line

The curl command line toollets you transfer data to or from a server, via one of several protocols. HTTP (and HTTPS) are two very common use cases.

By default, curl sends a GET request, as you can confirm with the following command:

you’re able to see the request method on the fifth line of the output:

You can change the request method that curl sends using the -X flag:

Now, curl sends a request using the POST method:

HTTP Has Many Uses

Most of what you do on the web will involve standard GET requests, with the occasional POST for form submissions. But HTTP is a much more expressive protocol than this suggests.

Some of the most common methods mirror the CRUD model which makes HTTP an excellent basis on which to build a resource-focussed API.

If you’re looking for an explanation of what a REST API is, and how you may use it, you’re in the right place.

My iPhone does it all, but I still need my dumb phone.

Your phone’s camera app doesn’t show this, so it’s easy to miss.

Goodbye sending links via other apps.

You can’t call this offline, Notion.

You’ve been quoting these famous films wrong all along!

Technology Explained

PC & Mobile