Sending PUT Request with Curl [PHP Code]

You can use the -X PUT command-line option to make an HTTP PUT request with Curl. PUT request data is passed with the -d command-line parameter. If you give -d and omit -X, Curl will automatically choose the HTTP POST method. The -X PUT option explicitly tells Curl to select the HTTP PUT method instead of POST. The data type for the Curl request is set using the -H command-line option. In this Curl PUT Example, we send a request to the ReqBin echo URL. Click Run to execute the Curl PUT request online and see the results. The PHP code was automatically generated for the Curl PUT example.
Sending PUT Request with Curl [PHP Code] Run
curl -X PUT https://reqbin.com/echo/put/json
    -d "PUT request data"
Updated: Viewed: 70414 times
PHP code for Curl PUT example

PHP code for Curl PUT Example

This PHP 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:

Curl PUT Request Format
curl -X PUT [URL]
   -H "Content-Type: [content type]" 
   -d "[request data]"

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.

Content-Type Examples
Content-Type: application/json
Content-Type: text/html; charset=UTF-8
Content-Type: multipart/form-data; boundary=any-string

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:

Basic Curl PUT request example
curl -X PUT https://reqbin.com/echo/put/json
    -H "Content-Type: application/json" 
    -d '{"key": "value"}'

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.

Curl PUT JSON Example
curl -X PUT https://reqbin.com/echo/put/json
    -H "Content-Type: application/json" 
    -d '{"key": "value"}'

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.

Curl PUT Request with Raw Data Example
curl -X PUT https://reqbin.com/echo/put/json
    -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
    -H "Content-Type: application/json"
    -d '{"key": "value"}'

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.

Curl PUT Request with Raw Data Example
curl -X PUT https://reqbin.com/echo/put/json
    --data-raw "Raw data"

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.

Curl PUT Request with Form Data Example
curl -X PUT https://reqbin.com/echo/put/json
    -F "[email protected]" 

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:

Curl PUT Request with Data From a File Example
curl -X PUT https://reqbin.com/echo/put/json
    -d @path/to/file.json

The following is an example of a PUT request with data from a file using the -F flag in Curl:

Curl PUT Request with Data From a File Example
curl -X PUT https://reqbin.com/echo/put/json
    -F "file=@path/to/file.json"

See also

Generate code snippets for PHP and other programming languages

Convert your Curl PUT request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the PHP code generator.