The HTTP/1.1 specification defines a number of methods that indicate the action that a given resource should take upon receipt of a request. These are also known as 'HTTP Verbs'.
Requests can be idempotent, meaning that they can be executed multiple times and you should see the same response each time, and requests can also be safe, meaning that the request should only retrieve data and not complete any other action.
The table below outlines the most common methods along with with some example use cases.
Method | Description | Idempotent | Safe | Example Use |
GET | Retrieve a URI | ✓ | ✓ | Loading a webpage |
HEAD | Retrieve a URI without the response body | ✓ | ✓ | Useful for debugging, or querying a resource to find metadata such as the size. |
POST | Submit data to a resource and create a new entity | ✘ | ✘ | Creating an article on a blog. |
PUT | Update all data in a resource entity | ✓ | ✘ | Updating all fields in an article on a blog (title and body). |
PATCH | Update some data in a resource entity | ✘ | ✘ | Updating only specific fields in an article on a blog (title only). |
DELETE | Remove a resource | ✓ | ✘ | Remove an article on a blog. |
Note: HTTP methods are not always implemented as per the specification. For example, you may find that an API specifies using POST to update some or all data for a specific resource, or that you need to execute a GET request to a specific URI to DELETE a resource.
Using cURL with Request Methods
cURL is a tool used to transfer data via multiple network protocols. It's extremely useful as a debugging tool and can handle all the HTTP Request Methods outlined in this article, and is capable of other protocols too. Let's look at how we can complete the above Request Methods with cURL.
GET
As GET requests are so common, executing one with cURL is as simple as providing the URL.
curl https://www.stackpath.com/
This will return the content of the URL to your console. If you want to save this to file, or have provided the URL of a binary file you can use the -o switch with the filename as the argument:
curl https://www.stackpath.com/ -o stackpath.com.html
Note that cURL will not follow redirects without being told to do so. You can enable this with the -L switch like below:
curl -L https://stackpath.com/ -o stackpath.com.html
HEAD
To execute a HEAD request use the -I switch. This will return only the headers and no content:
curl -I https://www.stackpath.com/
POST
If you're POSTing data to a URL you can use the -X switch with the argument POST to tell cURL to execute a POST request, and you can provide data with the -d switch. This will be URL encoded by cURL, if you need to send JSON, binary, or other data types you can check the cURL HTTP POST documentation for more examples.
curl -X POST -d name=admin -d age=30 http://api.example.com/user
PUT
PUT requests are similar to POST requests. Simply set the method to PUT and send all the required data:
curl -X PUT -d name=admin -d age=31 http://api.example.com/user/1
PATCH
PATCH requests are similar to PUT and POST requests. Simply set the method to PATCH and send the data you require:
curl -X PATCH -d age=32 http://api.example.com/user/1
DELETE
DELETE requests simply require you to set the method to DELETE via the -X switch.
curl -X DELETE http://api.example.com/user/1