Sending a Curl PATCH request with JSON

To send an HTTP PATCH request with JSON data using Curl, you need to 1) tell Curl to send a PATCH request by specifying the -X PATCH command line switch, 2) pass the JSON data with the -d command line switch, and 3) provide the correct ContentType HTTP header with command line switch -H. The -X PATCH command line switch tells Curl to use the HTTP PATCH method instead of POST. The -H 'Content-Type: application/json' command line switch tells Curll to send an HTTP header to the server indicating the data type in the PATCH message's body. In this Curl PATCH Request with JSON example, we send an HTTP PATCH request to the ReqBin echo URL. Click Run to execute the Curl PATCH request online and see the results.
Sending a Curl PATCH request with JSON Run
curl -X PATCH https://reqbin.com/echo/patch/json
     -H 'Content-Type: application/json'
     -H 'Accept: application/json'
     -d '{"Id": 78912, "Customer": "Jason Sweet", "Quantity": 1}'
Updated: Viewed: 20232 times

What is Curl?

Curl is a command-line utility for transferring data to or from a remote server. Curl is used for API testing, sending files to the server, and viewing the server response headers. Curl supports over 25+ protocols, including HTTP, HTTPS, FTP, FTPS, and SFTP, has built-in support for SSL certificates, HTTP Cookies, and more.

What is HTTP PATCH?

The HTTP PATCH method is one of 9 commonly used Hypertext Transfer Protocol (HTTP) request methods to modify an existing resource partially. The PATCH method sends an object containing partial data and rules for applying this data to an existing resource identified by its URI. For a PATCH request, we don't need to provide all the data. We only send the data that we want to update. The main difference from the HTTP PUT method is that the HTTP PATCH method partially replaces the resource, while the HTTP PUT method completely replaces the resource. PATCH is somewhat similar to the concept of "update" in CRUD.

Curl PATCH Request Syntax

The general form of the Curl command for sending a PATCH request is as follows:

Curl PATCH Request Syntax
curl -X PATCH [URL] 
     -H [header]
     -d [patch_data]

Where:
  • -X: the HTTP request method to be used
  • -H: an additional header to sent
  • -d: the data to be send with the PATCH request

How to send a PATCH request using Curl?

To make a PATCH request using Curl, you need to use the -X command-line option, specify the PATCH method, and pass the data type in the request's body.

Curl PATCH Request Example
curl -X PATCH https://reqbin.com/echo/patch/json
     -H 'Content-Type: application/json'
     -H 'Accept: application/json'
     -d '{"User": "Leo", "Id": 1}'

How do I send JSON using Curl?

To send JSON using Curl, you need to pass the JSON data with the -d '{"Id": 1, "Price": 25}' command line parameter and specify the correct MIME data type in the request body using the -H 'Content-Type: application/json' command line parameter.

Curl PATCH Request Examples

Example of sending JSON data with PATCH request method using Curl:

Curl PATCH Request with JSON data
curl -X PATCH https://reqbin.com
     -H "Content-Type: application/json"
     -d '{"Id": 1, "Price": 25}'

Example of sending a plain text with PATCH request method using Curl:

Curl PATCH Request with TEXT data
curl -X PATCH https://reqbin.com
     -H "Content-Type: text/plain"
     -d 'some text'

HTTP requests with JSON data

JSON data can be sent using the HTTP POST, PUT and PATCH request methods. To send JSON data to the server, you need to set the appropriate content type for the request body. If your client expects JSON from the server, it should also send the Accept: application/json request header. The server informs the client that it has returned JSON with a Content-Type: application/json response header.

Content-Type: application/json
Accept: application/json

See also

Generate Code Snippets for Curl PATCH Request JSON Example

Convert your Curl PATCH Request JSON request to the PHP, JavaScript/AJAX, Curl/Bash, Python, Java, C#/.NET code snippets using the ReqBin code generator.