Java code for Curl PUT ExampleThis Java code snippet was generated automatically for the Curl PUT example.
<< Back to the Curl PUT example
What is HTTP?
HTTP (Hypertext Transfer Protocol) is a network protocol for exchanging data between two network devices, widely used to transfer data between HTTP clients (browser or mobile application) and a server. HTTP is built on messages called "request" and "response". Each HTTP message consists of a request line, HTTP headers, and a message body.
What is the HTTP PUT method used for?
HTTP PUT is one of the commonly used methods of the HTTP protocol. The PUT method creates a new resource on the server or replaces an existing resource with the request payload. Unlike the PATCH method, the PUT request data must contain the complete representation of the resource. The HTTP PUT method is defined as idempotent, which means that multiple identical HTTP PUT requests must have the same effect as a single request.
How to send data using the PUT method with Curl?
You can pass data to Curl using the -d or -F command-line options as with any other Curl request. Note that Curl will automatically select application/x-www-form-urlencoded content type for the -d command line parameter and multipart/form-data for the -F command line parameter unless you explicitly specify your custom Content-Type using the parameter command line -H. For example, to send JSON data to the server, you need to use the -H "Content-Type: application/json" command-line parameter to explicitly specify the data type in your PUT request.
Curl PUT Request Syntax
The general form of a Curl command for making a PUT request is as follows:
- -X PUT: indicates the HTTP PUT request method.
- -H: the HTTP header to send to the server with the PUT request.
- -d: data to be sent to the server with the PUT request.
How to make a PUT request using Curl with a JSON body?
When making a PUT request with JSON data, you must pass the -H "Content-Type: application/json" header to Curl to indicate the data type in the body of the PUT message. This header is vital and allows the server to interpret and process the received JSON data correctly.
What is Content-Type?
The Content-Type HTTP header is used to indicate the media type of the resource in the body of the Curl request. For the server response, the Content-Type header tells Curl the type of data in the response body.
Why do I need to specify Content-Type for Curl PUT requests explicitly?
If you submit data using Curl and do not explicitly specify the data type, Curl will use application/x-www-form-urlencoded for your content. Therefore, when sending JSON, you must explicitly select the data type using the -H "Content-Type: application/json" command-line switch. If you don't set the correct Content-Type, your app may not work. For example, if the server can accept XML and JSON data on the same API endpoint, setting the Content-Type to application/json will let the server know that the data came in JSON format.