Node.js 不获取请求头授权令牌
我想用axios将数据发送到我的后端。但是请求头['Authorization']未定义。它不在标题中,尽管我使用axios发送它 axios:Node.js 不获取请求头授权令牌,node.js,react-native,axios,expo,Node.js,React Native,Axios,Expo,我想用axios将数据发送到我的后端。但是请求头['Authorization']未定义。它不在标题中,尽管我使用axios发送它 axios: import axios from 'axios'; const entryGroup = async (privatekey, userid) => { try { const options = { headers: { 'Authorization': `Bearer ${userid}`,
import axios from 'axios';
const entryGroup = async (privatekey, userid) => {
try {
const options = {
headers: {
'Authorization': `Bearer ${userid}`,
'Accept': 'application/json',
},
body: privatekey
};
return axios.post('http://xxxxx:3000/entrygroup', options).then(res => res.data).catch(e => e);
} catch(e) {
return e;
}
};
export default entryGroup;
Nodejs:(所有COR均已启用)
来自req.HEADER的输出:
{
accept: 'application/json, text/plain, */*',
'content-type': 'application/json;charset=utf-8',
'content-length': '127',
host: 'xxxx:3000',
connection: 'Keep-Alive',
'accept-encoding': 'gzip',
'user-agent': 'okhttp/3.14.9'
}
像这样发布你的请求
import axios from "axios";
const entryGroup = async (privatekey, userid) => {
try {
return axios.post(
"http://xxxxx:3000/entrygroup",
{ body: privatekey },
{
headers: {
Authorization: `Bearer ${userid}`,
Accept: "application/json",
},
}
).then(res => res.data).catch(e => e);
} catch (e) {
return e;
}
};
export default entryGroup;
然后在后端,你会得到它
console.log(req.headers)
不,不在那里…你把尸体弄对了吗??我的意思是你是否在Node.js中获得了
privatekey
?你是否使用了Express?是的,我使用了Express,我正确地获得了正文我显示了我的请求标题输出我在上面编辑了我的代码
console.log(req.headers)