How to Send an HTTP PUT Request Using Curl

This article provides a quick and clear guide on how to send an HTTP PUT request using the curl command-line tool. You will learn the correct command syntax, how to send data payloads in different formats, and how to upload files directly to a server using PUT.

Basic Syntax for PUT Requests

To send a PUT request with curl, you use the -X PUT (or --request PUT) option followed by the target URL.

The basic command structure is:

curl -X PUT URL

Sending a PUT Request with JSON Data

HTTP PUT requests are frequently used to update existing resources on a server, often requiring a data payload. To send JSON data, you must define the request method with -X PUT, set the Content-Type header using -H, and pass the data using the -d option.

curl -X PUT -H "Content-Type: application/json" -d '{"name": "John Doe", "email": "john@example.com"}' https://api.example.com/users/1

Sending Data from a Local File

If your data payload is large or already stored in a file, you can instruct curl to read the data from that file using the @ symbol followed by the file path.

curl -X PUT -H "Content-Type: application/json" -d @data.json https://api.example.com/users/1

Uploading a File Using PUT

If you want to upload a physical file (like an image or a text file) to a server using a PUT request, curl provides a convenient shortcut using the -T (or --upload-file) flag. When you use -T, curl automatically changes the request method to PUT.

curl -T document.pdf https://api.example.com/uploads/document.pdf