Sending HTTP PATCH Requests With curl
In this snippet, we are going to be taking a look at how we can send HTTP PATCH
requests using the curl
command line tool.
The PATCH
HTTP verb is used for partial update endpoints. For example, if I just wanted to update a few of he values within an object, I would tend to use a PATCH
request with the ID of the object I wish to update, and the underlying API would then take care of updating these values for me.
Sending a HTTP PATCH Request with Curl
The curl tool takes in a flag -X
which allows you to specify the HTTP verb you wish to use for the
request. In this case, we want to specify curl -X PATCH
to signifiy this is an HTTP PATCH
request.
$ curl -X PATCH http://api:port/api/v1/path
Adding A PATCH Body
Now, typically we are sending data alongside the PATCH request to our API endpoint. In order to do this, we can
use the -d
flag in order to specify the data we wish to send:
$ curl -d '{"new": "value"}' -X PATCH http://api:port/api/v1/path
Specifying Headers With Curl
We can specify headers in our curl requests using the -H
like this:
$ curl -d '{"new": "value"}' -H 'Content-Type: application/json' -X PATCH http://api:port/api/v1/path