Created
February 2, 2021 13:14
-
-
Save toddlers/c89958d9a3db93a75c42c9a6a678dfc2 to your computer and use it in GitHub Desktop.
nodejs http post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://blog.bearer.sh/node-http-request/ | |
//https://nodejs.org/en/knowledge/HTTP/clients/how-to-create-a-HTTP-request/ | |
const http = require("http") | |
let body = JSON.stringify({ | |
title: "Make a request with Node's http module" | |
}) | |
let options = { | |
hostname: "postman-echo.com", | |
path: "/post", | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
"Content-Length": Buffer.byteLength(body) | |
} | |
} | |
http | |
.request(options, res => { | |
let data = "" | |
res.on("data", d => { | |
data += d | |
}) | |
res.on("end", () => { | |
console.log(data) | |
}) | |
}) | |
.on("error", console.error) | |
.end(body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment