http_post.js
// ---------------------------------------------------------------
// http_post.js
//
// Apr/08/2023
//
// ---------------------------------------------------------------
import https from "https"
const data = JSON.stringify({user: "jiro", password: "123456"})
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
'Content-Length': data.length
}
}
const url = "https://httpbin.org/post"
const request = https.request(url, options, response => {
console.log(`statusCode: ${response.statusCode}`)
response.on('data', (dd) => {
process.stdout.write(dd)
})
})
request.write(data)
request.end()
// ---------------------------------------------------------------
package.json
{
"type": "module"
}
実行結果
$ node ./http_post.js
statusCode: 200
{
"args": {},
"data": "{\"user\":\"jiro\",\"password\":\"123456\"}",
"files": {},
"form": {},
"headers": {
"Content-Length": "35",
"Content-Type": "application/json",
"Host": "httpbin.org",
"X-Amzn-Trace-Id": "Root=1-621b0c76-6f84d16d48a282e0407c6bcb"
},
"json": {
"password": "123456",
"user": "jiro"
},
"origin": "219.126.139.79",
"url": "https://httpbin.org/post"
}
次のバージョンで確認しました。
$ node --version
v22.3.0