LoginSignup
16
15

More than 5 years have passed since last update.

NodeJSでHttps/GETでJsonを取得して利用する

Posted at

やること

  • Json を返す Https な API を叩く
  • 返ってきたJsonをパースしてJSから利用する

具体的になにやるの?

Bitcoin のレート情報を取得できるAPIがあるのでそれを利用して現在のレート情報を取得します。

取引所 API 概要

返り値

{
  "rate": "60000"
}

コード

const https = require('https');
// Bitcoin のレートを json で取得することができるAPI
const URL = "https://coincheck.com/api/rate/btc_jpy";

https.get(URL, function (res) {
    var body = '';
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        body += chunk;
    });
    res.on('data', function (chunk) {
        // body の値を json としてパースしている
        res = JSON.parse(body);
        console.log(`現在のレートは${res.rate}円/Bitcoinだよ!!`);
    })
  }).on('error', function (e) {
    console.log(e.message);
});

実行結果

2018-01-15_22-43-09[1].png

16
15
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
16
15