Send HTTP PATCH Request with cURL
This article provides a quick guide on how to send an HTTP PATCH
request using the curl command-line tool. You will learn
the exact syntax required to perform partial updates on a resource,
including how to set the request method, send JSON data payloads, define
headers, and handle authentication.
The Basic cURL PATCH Syntax
To send a PATCH request, you must use the -X (or
--request) option followed by PATCH. By
default, curl performs GET requests, so specifying the
method is necessary.
curl -X PATCH https://api.example.com/resource/1Sending JSON Data with PATCH
HTTP PATCH requests are commonly used to send partial updates to an
API in JSON format. To do this, you need to: 1. Set the
Content-Type header to application/json using
the -H option. 2. Provide the JSON payload using the
-d (or --data) option.
Here is a complete example updating a user’s email address:
curl -X PATCH https://api.example.com/users/123 \
-H "Content-Type: application/json" \
-d '{"email": "newemail@example.com"}'Adding Authentication Headers
Most modern APIs require authentication. You can pass authorization
tokens, such as Bearer tokens, by adding an additional -H
header option.
curl -X PATCH https://api.example.com/users/123 \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"status": "active"}'Sending PATCH Data from a Local File
If your JSON payload is too large or complex to type directly into
the terminal, you can store it in a local JSON file (e.g.,
data.json) and reference it in your command using the
@ symbol.
curl -X PATCH https://api.example.com/users/123 \
-H "Content-Type: application/json" \
-d @data.json