nodejs axios。发送https获取请求时发生错误

发布于 2025-01-30 06:28:03 字数 1154 浏览 1 评论 0原文

我正在使用Axios版本0.21.1
我正在发送https获取请求如下。

当我在代码下运行时,try块中的#2行console.log ...抛出错误。
但是我在catch中获得的错误对象是空的。不确定为什么日志丢弃错误。

try {
    let getRes = await axios.get(myUrl, {headers: {'Authorization': `Bearer ${token}`}});
    console.log("getRes: " + JSON.stringify(getRes));
} catch (error) {
    console.log("Error: " + JSON.stringify(error));
}

如果我使用#2参数为{}null for axios.get
。 我在catch中收到错误,但我不确定为什么会失败。

try {
    let getRes = await axios.get(myUrl, {}, {headers: {'Authorization': `Bearer ${token}`}});
    console.log("getRes: " + JSON.stringify(getRes));
} catch (error) {
    console.log("Error: " + JSON.stringify(error));
}

我遇到的错误是401未经授权
从Postman中,此URL与我从代码中使用的相同bearer令牌工作正常。

我什至尝试了以下代码,其行为与#1案例相同:

let getrRes = await axios({
    method: 'get',
    url: myUrl,
    headers: {
        "Authorization": "Bearer "+token
    }
});

我不想让请求件为此get请求。
有什么问题以及如何正确调用axios.get

I am using the Axios version 0.21.1.
I am sending HTTPS get request as below.

When I run below code, the #2 line inside the try block console.log... throws error.
But the error object I get in catch is empty. Not sure why log is throwing error.

try {
    let getRes = await axios.get(myUrl, {headers: {'Authorization': `Bearer ${token}`}});
    console.log("getRes: " + JSON.stringify(getRes));
} catch (error) {
    console.log("Error: " + JSON.stringify(error));
}

If I run the following version with #2 parameter as {} or null for axios.get.
I get the error printed in catch but I am not sure why it is failing.

try {
    let getRes = await axios.get(myUrl, {}, {headers: {'Authorization': `Bearer ${token}`}});
    console.log("getRes: " + JSON.stringify(getRes));
} catch (error) {
    console.log("Error: " + JSON.stringify(error));
}

The error I get is 401 Unauthorized.
From the Postman, this URL is working fine with the same Bearer token as I am using from the code.

I have even tried the below code, with the behavior same as #1 case:

let getrRes = await axios({
    method: 'get',
    url: myUrl,
    headers: {
        "Authorization": "Bearer "+token
    }
});

I don't want to have request body for this get request.
What can be the issue and how to call axios.get correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟柳画桥 2025-02-06 06:28:03

您的第一个程序是正确的程序!但是内部是您的console.log()不好的:您不能在json.stringify()方法上使用getres axios返回的对象 axios ,这就是为什么您的程序进入捕获量的原因。

要显示响应,要么不使用json.stringify(),要么在Axios返回的数据上使用json.stringify()

try {
    let getRes = await axios.get(myUrl, {headers: {'Authorization': `Bearer ${token}`}});
    console.log("getRes: " + JSON.stringify(getRes.data));
    // OR
    console.log("getRes: " + getRes);
} catch (error) {
    console.log("Error: " + error);
}

请注意,您也不能在捕获中遇到的错误上使用json.stringify()!这就是为什么您只有一个空的对象。

Your first program is the right one! But inside it's your console.log() that is not good: you cannot use the JSON.stringify() method on the getRes object returned by axios, that's why your program goes into the catch.

To display the response, either don't use JSON.stringify(), or use JSON.stringify() on the data returned by axios (which is getRes.data)

try {
    let getRes = await axios.get(myUrl, {headers: {'Authorization': `Bearer ${token}`}});
    console.log("getRes: " + JSON.stringify(getRes.data));
    // OR
    console.log("getRes: " + getRes);
} catch (error) {
    console.log("Error: " + error);
}

Note that you can't use the JSON.stringify() on the error that you got in the catch either! That's why you only had an empty object.

合约呢 2025-02-06 06:28:03

如果要确定错误原因,请在尝试捕获块中更改console.log。不要尝试json.strigify错误,而只是将其转移到控制台上。这将确保将错误 as-is 丢弃到控制台上。

try {
  let getRes = await axios.get(myUrl, {headers: {'Authorization': `Bearer ${token}`}});
  console.log("getRes: " + JSON.stringify(getRes));
} catch (error) {
  console.log(error);
}

如果您仍然希望收到一条细粒度错误消息,则可以使用捕获子句中的以下语句之一将错误转换为字符串:


console.log(error.message);
console.log(error.toString());

If you want to identify the exact cause of error, change the console.log in your try catch block. Don't try to JSON.strigify the error, rather just dump it onto the console. This will make sure to dump the error as-is, onto the console.

try {
  let getRes = await axios.get(myUrl, {headers: {'Authorization': `Bearer ${token}`}});
  console.log("getRes: " + JSON.stringify(getRes));
} catch (error) {
  console.log(error);
}

If you still wish to get a fine grained error message, you can convert the error into string by using one of the following statements inside your catch clause:


console.log(error.message);
console.log(error.toString());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文