首页 > 解决方案 > 得到 - HTTPS 请求 - HTTPError

问题描述

我正在尝试使用下面的代码段访问受基本身份验证保护的 URL。

const got = require('got');
export const getToken = async () => {

    const payload = new URLSearchParams({grant_type: 'client_credentials'}).toString();
    const auth = "Bearer " + Buffer.from(process.env.username + ":" + process.env.password).toString('base64');
    try{
        const response = await got(process.env.url, {
            body: payload,
            method: "POST",
            headers: {
                "Authorization" : auth,
                "Content-Type": "application/x-www-form-urlencoded",
                "Content-Length": payload.length.toString()
            }
        });
    }catch(error){
        console.log(`ERROR: ${JSON.stringify(error)}`);
    }
    
};

但这会导致错误,也没有提供任何细节。

{"name":"HTTPError","timings":{"start":1634484011218,"socket":1634484011219,"lookup":1634484011230,"connect":1634484011248,"secureConnect":1634484011288,"upload":1634484011288,"response":1634484011307,"end":1634484011310,"abort":1634484011314,"phases":{"wait":1,"dns":11,"tcp":18,"tls":40,"request":0,"firstByte":19,"download":3,"total":96}}}

任何有关解决问题的建议将不胜感激。谢谢。

标签: node.jshttpsbasic-authentication

解决方案


推荐阅读