LoginSignup
77
73

More than 3 years have passed since last update.

Node.js:JSONデータをPOSTする

Last updated at Posted at 2016-06-22

※「≈≈≈」は省略を表す

バージョン

$ node -v
v6.2.1
$ cat package.json
{
  ≈≈≈
  "dependencies": {
    "request": "~2.72.0",
  }
}

POSTでJSONデータを送信する

var request = require('request');
var options = {
  uri: "http://example.com/test",
  headers: {
    "Content-type": "application/json",
  },
  json: {
    "key1": "param1",
    "key2": "param2"
  }
};
request.post(options, function(error, response, body){});

おまけ:POSTでパラメータを送信する(いわゆるフツーのPOST)(未検証)

var request = require('request');
var options = {
  uri: "http://example.com/test",
  headers: {
    "Content-type": "application/x-www-form-urlencoded",
  },
  form: {
    "key1": "param1",
    "key2": "param2"
  }
};
request.post(options, function(error, response, body){});
77
73
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
77
73