Sending PUT Request with Curl [Node.js 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 Node.js code was automatically generated for the Curl PUT example.
Sending PUT Request with Curl [Node.js Code] Run
curl -X PUT https://reqbin.com/echo/put/json
     -d "PUT request data"
Updated: Viewed: 44163 times
Node.js code for Curl PUT example

Node.js code for Curl PUT Example

This Node.js 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:

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

Where:
  • -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.

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

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 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.

See also

Generate code snippets for Node.js 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 Node.js code generator.

// try get IpInfo ASAP! getIpInfo($.noop);