如何在 api 中发送 headers 而 body 为空,在 React js 中使用 axios。?
How to send headers in api with body empty using axios in react js.?
我正在尝试发送一个 api 调用,在 header 中使用访问令牌并且为空 body 我正在发送确切的方式,但由于某些原因它一直在提供我的错误 401 是未经授权的,我找不到其中的错误,我无法解决它。
我的API电话
export const resendSmsCode = (userdata) => {
return async (dispatch) => {
dispatch(fullScreenLoader(true));
const response = await axios.post(
"https://theappsouk.com/api/Profile/ReGenerateSmsCode",
{
Authorization: `Bearer ${userdata.auth}`,
}
);
const { data } = response;
console.log(JSON.stringify(response))
dispatch(fullScreenLoader(false));
dispatch({
type: RESEND_SMS_CODE,
payload: data,
});
};
};
看起来 header 选项应该放在第三个参数中。但是你把它放在第二个参数上,它将你的 header 作为有效载荷发送。
正确的是:
const response = await axios.post(
"https://theappsouk.com/api/Profile/ReGenerateSmsCode",
void 0,
{
Authorization: `Bearer ${userdata.auth}`,
}
);
如果你想发送headers,你必须这样写:
const response = await axios.post(
"https://theappsouk.com/api/Profile/ReGenerateSmsCode",
{
headers: {
Authorization: `Bearer ${userdata.auth}`
}
}
);
PS:发布问题时,我会涂黑身份验证令牌或密码(如您的屏幕截图所示)。只需确保使此令牌无效即可。
我正在尝试发送一个 api 调用,在 header 中使用访问令牌并且为空 body 我正在发送确切的方式,但由于某些原因它一直在提供我的错误 401 是未经授权的,我找不到其中的错误,我无法解决它。
我的API电话
export const resendSmsCode = (userdata) => {
return async (dispatch) => {
dispatch(fullScreenLoader(true));
const response = await axios.post(
"https://theappsouk.com/api/Profile/ReGenerateSmsCode",
{
Authorization: `Bearer ${userdata.auth}`,
}
);
const { data } = response;
console.log(JSON.stringify(response))
dispatch(fullScreenLoader(false));
dispatch({
type: RESEND_SMS_CODE,
payload: data,
});
};
};
看起来 header 选项应该放在第三个参数中。但是你把它放在第二个参数上,它将你的 header 作为有效载荷发送。
正确的是:
const response = await axios.post(
"https://theappsouk.com/api/Profile/ReGenerateSmsCode",
void 0,
{
Authorization: `Bearer ${userdata.auth}`,
}
);
如果你想发送headers,你必须这样写:
const response = await axios.post(
"https://theappsouk.com/api/Profile/ReGenerateSmsCode",
{
headers: {
Authorization: `Bearer ${userdata.auth}`
}
}
);
PS:发布问题时,我会涂黑身份验证令牌或密码(如您的屏幕截图所示)。只需确保使此令牌无效即可。