Sending POST Request [Node.js Code]

The HTTP POST method sends data to the server to create/update a resource on the server. The POST data is included in the body of the Node.js POST message. The Content-Type request header indicates the data type in the POST body, and the Content-Length request header indicates the data size in the Node.js POST request. In this Node.js POST Request example, we send data to the ReqBin echo URL with Content-Type and Content-Length HTTP headers. Click Send to execute the Node.js POST Request example online and see the results. The Node.js code was automatically generated for the POST Request example.
Sending POST Request [Node.js Code] Send
POST /echo/post/json HTTP/1.1
Host: reqbin.com
Content-Type: application/json
Content-Length: 80

{
  "Id": 12345,
  "Customer": "John Smith",
  "Quantity": 1,
  "Price": 10.00
}
Updated: Viewed: 70777 times
Node.js code for POST Request example

Node.js code for POST Request Example

This Node.js code snippet was generated automatically for the POST Request example.
<< Back to the POST Request example

What is HTTP?

The Hypertext Transfer Protocol (HTTP) is the core of the World Wide Web and provides communication between HTTP clients and servers. HTTP works as a request-response protocol between a client and a server in a format that both HTTP clients and servers can understand. For example, when a user uploads a document to the server, the browser sends an HTTP POST request and includes the document in the body of the POST message. After processing the client's POST request, the server returns a response to the browser and indicates whether the server accepts or rejects the document, with a response status code and message.

What is the HTTP POST request method used for?

The HTTP POST request method is used to send data to the server or create or update a resource. The Node.js POST request is usually used when submitting an HTML form or uploading data to a server. The HTTP POST request may or may not contain data. The data is sent to the server in the body of the Node.js POST request message. The Content-Type header indicates the data type in the body of the POST request, and the data length is indicated with the Content-Length HTTP header.

Some notes on HTTP POST Requests

  • POST requests are used to create or update a resource on the server.
  • POST messages can contain any type of data of unlimited size.
  • POST method is not idempotent, which means that sending the same POST request multiple times can further affect the server's state.

HTTP POST Request Examples

Below are examples of HTTP POST requests:

How to post HTML Form Data to the server?

The following is an example of an HTTP POST request message for submitting an HTML form to the server:

POST HTML Form Example
POST /echo/post/form HTTP/1.1
Host: reqbin.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 23
      
key1=value1&key2=value2

How to post JSON data to the server?

The following is an example of an HTTP POST request message for sending JSON data to the server:

POST JSON Data Example
POST /echo/post/json HTTP/1.1
Host: reqbin.com
Accept: application/json
Content-Type: application/json
Content-Length: 81
      
{
  "Id": 78912,
  "Customer": "Jason Sweet",
  "Quantity": 1,
  "Price": 18.00
}

How to post XML data to the server.?

The following is an example of an HTTP POST request message for sending XML data to the server:

POST XML Data Example
POST /echo/post/xml HTTP/1.1
Host: reqbin.com
Content-Type: application/xml
Accept: application/xml
Content-Length: 118
      
<?xml version="1.0" encoding="utf-8"?>
<Request>
    <Login>login</Login>
    <Password>password</Password>
</Request>

See also

Generate code snippets for Node.js and other programming languages

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