64
46

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Node.jsからWebAPIを叩く

Last updated at Posted at 2017-07-16

前提

GETあるいはPOSTでデータを渡しJSONでデータが帰ってくるものを想定したコードです。
また、叩く方法はたくさんありますが、比較的短く書ける request を使用します。

ソース

GET

const request = require('request');

var URL = 'URL_IS_HERE';

request.get({
	uri: URL,
	headers: {'Content-type': 'application/json'},
	qs: {
		// GETのURLの後に付く
		// ?hoge=hugaの部分
	},
	json: true
}, function(err, req, data){
	console.log(data);
});

POST

const request = require('request');

var URL = 'URL_IS_HERE';

request.post({
	uri: URL,
	headers: { "Content-type": "application/json" },
	json: {
		// JSONをPOSTする場合書く
	}
}, (err, res, data) => {
	console.log(data);
});
64
46
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
64
46

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?