Curl PATCH Request With JSON [Java Code]

To send an HTTP PATCH request with JSON data to the REST API endpoint using Curl, you need to make a PATCH request and provide JSON in the body of the message and a Content-Type: application/json request header that specifies the data type as JSON. To specify the correct MIME data type in the request's body, use the -H command-line option for Curl. The JSON data is passed with the -d command-line parameter. 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. The Java code was automatically generated for the Curl PATCH Request JSON example.
Curl PATCH Request With JSON [Java Code] 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: 15910 times
Java code for Curl PATCH Request JSON example

Java code for Curl PATCH Request JSON Example

This Java code snippet was generated automatically for the Curl PATCH Request JSON example.
<< Back to the Curl PATCH Request JSON example

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.

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. 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 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 Java and other programming languages

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