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. 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 -H parameter. 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:
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 web application 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/xml will let the server know that the data came in XML format.
Curl PUT Request Examples
The following are examples of sending a PUT request to Curl:
Basic Curl PUT request example
The following is a basic example of sending a PUT request to the ReqBin echo URL:
Sending a PUT request 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.
Sending PUT request with Authentication Token
To send a PUT request with a Bearer Token, you can use the -H header with the "Authorization" key, which specifies the authentication method.
Sending raw data with a PUT request
To send raw data with a PUT request with Curl, you can use the --data-raw command line option. The --data-raw flag in the Curl command sends the data to the server in the request body without additional processing or modification.
Sending a PUT request with form data
To send a PUT request with form data, you can use the -F flag to specify that you're sending data in multipart/form-data format.
Sending a PUT request with data from a file
To send a PUT request with data from a file, you can use the -d or -F flag to specify the path to the file. Curl will automatically try to determine the Content-Type based on the file extension. If the file has a .json extension, Curl will assume it contains JSON data and set the Content-Type to application/json in the header. The following is an example of a PUT request with data from a file using the -d flag in Curl:
The following is an example of a PUT request with data from a file using the -F flag in Curl: