LoginSignup
4
6

More than 3 years have passed since last update.

JavaScriptのrequestで自己署名証明書のWebサーバーに接続する

Last updated at Posted at 2019-09-23

社内や開発用のWebサーバーには自己署名証明書がインストールされていることが少なくない。
自己署名証明書のWebサーバーにJavaScriptのrequestでアクセスすると、「Error: self signed certificate」とメッセージが表示される。
これを回避する方法を検索しても、明確な情報が直ぐに見つけられなかったので、調べたことを記して置こう。

証明書エラーを無視するオプション

以下の2通りあることが分かった。

  • options.rejectUnauthorized = false を指定
  • options.agentOptions.rejectUnauthorized = false を指定

Github の request のREADME.mdに記載されている tls API doc for TLS/SSL options 近辺を読むと、後者の方が正式なようにも思えるが、実際のところは分からない。

使用例

var request = require('request');

function request_hoge(callback)
{
    var options = {
        url: 'https://localhost:8443/hoge',
        method: 'GET',
        agentOptions: {
            rejectUnauthorized: false,
        },
    };
    request(options, callback);
}

async function hoge()
{
    await request_hoge(function(err, res, body) {
        if (err) {
            console.log(err);
        }
        else if (res.statusCode != 200) {
            console.log(res);
        }
        else {
            console.log(body);
        }
    });
}

hoge();
4
6
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
6