nodejs axios。发送https获取请求时发生错误
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的第一个程序是正确的程序!但是内部是您的console.log()不好的:您不能在
json.stringify()
方法上使用getres
axios返回的对象 axios ,这就是为什么您的程序进入捕获量的原因。要显示响应,要么不使用
json.stringify()
,要么在Axios返回的数据上使用json.stringify()
请注意,您也不能在捕获中遇到的错误上使用
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 thegetRes
object returned byaxios
, that's why your program goes into the catch.To display the response, either don't use
JSON.stringify()
, or useJSON.stringify()
on the data returned by axios (which is getRes.data)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.如果要确定错误原因,请在尝试捕获块中更改
console.log
。不要尝试json.strigify
错误,而只是将其转移到控制台上。这将确保将错误 as-is 丢弃到控制台上。如果您仍然希望收到一条细粒度错误消息,则可以使用捕获子句中的以下语句之一将错误转换为字符串:
If you want to identify the exact cause of error, change the
console.log
in your try catch block. Don't try toJSON.strigify
the error, rather just dump it onto the console. This will make sure to dump the error as-is, onto the console.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: