gpt4 book ai didi

How to get a Authorization Bearer Token from Response Headers with Axios?(如何从Axios的响应头中获取授权承载令牌?)

转载 作者:bug小助手 更新时间:2023-10-27 19:46:25 33 4
gpt4 key购买 nike



The backend is sending me a Bearer token inside the response headers and I want to store it inside the redux store as most of the APIs require that token to be sent back inside the headers.

后端在响应标头中向我发送了一个持有者令牌,我希望将其存储在redux存储中,因为大多数API都需要将该令牌发送回标头中。


I'm encountering an issue when I'm attempting to save the token from the response because it comes as undefined.

我在尝试从响应中保存令牌时遇到问题,因为它是未定义的。


This is the code that I've got so far:

这是我到目前为止得到的代码:


  const onLogin = async () => {
try {
const response = await axiosInstance.post('/api/auth/login', userData);
push('/');
const token = response.headers['Authorization'];

console.log('token', token);

dispatch(userToken(token));
} catch (error: any) {
console.log('Login Error', error.message);
}
};

The login works. I get status code 200 and can also see the token inside the network tab in the console. But if I try to console.log it, it shows as undefined, so I imagine it's also not stored in the redux store.

登录起作用了。我得到状态代码200,并且还可以在控制台的网络选项卡中看到令牌。但是如果我尝试sole.log它,它显示为未定义,所以我认为它也没有存储在redux存储中。


How can I get that token and save it to the redux store?

我如何才能得到令牌并将其保存到Redux商店?


更多回答
优秀答案推荐

you can use like:

您可以使用LIKE:


const { Bearer } = response

if response has Bearer = .......

如果响应具有持有者=.....


you can get Bearer

你可以得到持有者


更多回答

Yeah... This doesn't work. I tried to console.log the response.headers and I got AxiosHeaders instead of the Response Headers. The Axios configuration also sets the base URL only. I don't know why I don't have access to the response headers...

是啊...这不管用我尝试console.log response.headers,得到的是AxiosHeaders而不是Response Headers。Axios配置还仅设置基本URL。我不知道为什么我不能访问响应头.

do you have response type?

您有回复类型吗?

what do you mean?

你是什么意思?

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com