3
1

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 1 year has passed since last update.

Node.js 外部APIを実行してレスポンスをコンソールに出力する

Last updated at Posted at 2022-12-28

概要

  • Node.jsにて外部APIを実行してレスポンスをコンソールに出力する方法をまとめる。

情報

使用する外部API

  • Qiitaのユーザー情報を取得するAPIを実行してみる。

  • どうやらhttps://qiita.com/api/v2/users/ユーザーidとすることでユーザーの公開されている基本情報を取得する事ができるっぽい。

  • 筆者の場合https://qiita.com/api/v2/users/miriwoとなる。念の為curlで実行してみたら下記の様に情報が帰ってきたのでこのURLにGETリクエストを送れば情報が取得できるっぽい。

    curl https://qiita.com/api/v2/users/miriwo
    {"description":"みらいのじぶんにむけてきじをかいてます\r\nコルチゾール駆動開発","facebook_id":"profile.php?id=100040336992140","followees_count":68,"followers_count":211,"github_login_name":"miriwo0104","id":"miriwo","items_count":1162,"linkedin_id":"","location":"japan","name":"大川 峻","organization":"株式会社アンテク","permanent_id":306417,"profile_image_url":"https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/306417/profile-images/1583802366","team_only":false,"twitter_screen_name":"mirimiripc","website_url":"https://miriwo.hatenablog.com/"}
    

方法

追加モジュールなし

  • 下記の様にコードを記載して実行すると情報を取得する事ができる。

    node.js
    import https from "https";
    
    const url = "https://qiita.com/api/v2/users/miriwo";
    
    https.get(url, (res) => {
        let body = '';
        console.log("statusCode:", res.statusCode);
        console.log("headers:", res.headers);
      
        res.on("data", function (d) {
          body += d;
        });
        
        res.on('end', function () {
          console.log(body);
        })
    })
    .on("error", (e) => {
        console.error(e);
    });
    
  • 上記を実行すると下記の様にコンソールに出力される。

    statusCode: 200
    headers: {cache-control: "max-age=0, private, must-revalidate", content-type: "application/json; charset=utf-8"}
    {"description":"みらいのじぶんにむけてきじをかいてます\r\nコルチゾール駆動開発","facebook_id":"profile.php?id=100040336992140","followees_count":68,"followers_count":211,"github_login_name":"miriwo0104","id":"miriwo","items_count":1162,"linkedin_id":"","location":"japan","name":"大川 峻","organization":"株式会社アンテク","permanent_id":306417,"profile_image_url":"https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/306417/profile-images/1583802366","team_only":false,"twitter_screen_name":"mirimiripc","website_url":"https://miriwo.hatenablog.com/"} 
    

追加モジュールあり

  • この方法は追加パッケージを使うのでWeb上のPlaygroundでは実行できない。

  • 下記の様にrequestパッケージをインストールする。

    npm install request
    
  • 下記の様に記載する。

    node.js
    import request from 'request';
    const url = 'https://qiita.com/api/v2/users/miriwo';
    
    const method = 'GET';
    const options = {
        url: url,
        method: method,
    };
    
    export const handler = (event) => {
        request(options, function (error, response, body) {
            console.log(body);
        });  
    };
    
  • 上記を実行すると下記の様に出力される。

    {"description":"みらいのじぶんにむけてきじをかいてます\r\nコルチゾール駆動開発","facebook_id":"profile.php?id=100040336992140","followees_count":68,"followers_count":211,"github_login_name":"miriwo0104","id":"miriwo","items_count":1163,"linkedin_id":"","location":"japan","name":"大川 峻","organization":"株式会社アンテク","permanent_id":306417,"profile_image_url":"https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/306417/profile-images/1583802366","team_only":false,"twitter_screen_name":"mirimiripc","website_url":"https://miriwo.hatenablog.com/"}
    

参考文献

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?