Posting JSON to the Server [Node.js Code]

To post JSON data to the server using Node.js, you need to provide the JSON data in the HTTP POST request body and pass the "Content-Type: application/json" request header. The Content-Type request header specifies the media type for the resource in the body. Additionally, you can pass an "Accept: application/json" header, which tells the server that the client is expecting JSON data. In this Node.js POST JSON example, we send JSON data to the ReqBin echo URL with the appropriate Accept and Content-Type HTTP headers. Click Send to execute the Node.js POST JSON example online and see the results. The Node.js code was automatically generated for the POST JSON example.
Posting JSON to the Server [Node.js Code] Send
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
}
Updated: Viewed: 76482 times
Node.js code for POST JSON example

Node.js code for POST JSON Example

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

What it JSON?

JavaScript Object Notation (JSON) is a textual, language-neutral data interchange format that defines a small set of formatting rules for the portable representation of structured data. JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays). JSON has been used to exchange data between applications written in many programming languages, including JavaScript, Java, C ++, C #, Go, PHP, Python, etc.

What is HTTP POST?

HTTP POST is a request method of an Hypertext Transfer Protocol (HTTP) is used to create or update a resource on the server. The HTTP POST method is used to submit web forms and files and images to the server.

What MIME type should I use when posting JSON data to the server?

According to RFC4627, the official media type for JavaScript Object Notation (JSON) objects is "application/json". When sending JSON data to the server using the HTTP POST, PUT, or PATCH methods, you must also add the "Content-Type: application/json" header to your request to tell the client about the data type in the request body. If the server returns JSON in its response, it must also provide a "Content-Type: application/json" response header so that the Node.js client can correctly understand and process the returned JSON.

Content-Type Header for JSON Syntax

The following is an example of a Content-Type header for JSON:

JSON Content-Type Header Syntax
Content-Type: application/json

How to request JSON data from the server?

If your client Node.js expects JSON from the server, it must also send the Accept: application/json request header. If you don't send the Accept header, the server may return data in a different format. For example, if the server can handle both JSON and XML requests on the same API endpoint, setting the Accept request header to application/json will let the server know that the Node.js client is expecting JSON and will provide the data in that format rather than XML.

POST JSON Example

The following is an example of sending JSON data to ReqBin echo URL.

POST JSON Request
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
}
  

The server response to our POST JSON request:

POST JSON Response
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 19
      
{"success":"true"}

See also

Generate code snippets for Node.js and other programming languages

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