4
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Node.js の http クライアントの使い方 (Post)

Last updated at Posted at 2018-05-04
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
4
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?