Making an HTTP POST request
Making a GET request with Node is trivial; in fact, HTTP GET requests been covered in Chapter 4, Using Streams in the context of stream processing.
HTTP GET requests are so simple that we can fit a request that prints to STDOUT into a single shell command (the -e flag passed to the node binary instructs Node to evaluate the string that follows the flag):
$ node -e "require('http').get('http://example.com', (res) => res.pipe(process.stdout))"
In this recipe, we'll look into constructing POST requests.
Getting ready
Let's create a folder called post-request, then create an index.js inside the folder, and open it in our favorite text editor.
How to do it...
We only need one dependency, the core http module. Let's require it:
const http = require('http') Now let's define a payload we wish to POST to an endpoint:
const payload = `{
"name": "Cian O Maidín",
"company": "nearForm"
}` Now we'll define the configuration object for the request we're about to make:
const...