node.js - 使用 axios 请求 node.js api 问题
问题描述
您好,我尝试访问 http://localhost:8080/api/v1/postulant 但我无法在标头中设置访问令牌。
let headers = {
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjUsImlhdCI6MTYxOTUyNDE4NywiZXhwIjoxNjIwMTI4OTg3fQ.hSXARB-y7rClswYZ380HV5RW77qjYNt5FzW2NfDd8Vw',
'Content-Type': 'application/json',
}
axios.get("http://localhost:8080/api/v1/postulant", { headers })
在我使用的 api 上:
app.get('/api/v1/postulant',Auth,function (req,res){
console.log(req.headers)
db.query('SELECT * FROM postulant_view', (err, rows) => {
if (err) { console.log(err); }
else {
console.log(rows)
let result = rows;
res.json(result);
}
})
并且我的请求的标题给出了:[
我尝试过:
app.get('/test',Auth,function (req,res){
console.log(req.headers)
db.query('SELECT * FROM postulant_view', (err, rows) => {
if (err) { console.log(err); }
else {
console.log(rows)
let result = rows;
res.json(result);
}
})
并打电话给
let headers = {
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjUsImlhdCI6MTYxOTUyNDE4NywiZXhwIjoxNjIwMTI4OTg3fQ.hSXARB-y7rClswYZ380HV5RW77qjYNt5FzW2NfDd8Vw',
'Content-Type': 'application/json',
}
axios.get("http://localhost:8080/test", { headers })
所以它有效有人解释为什么第二个有效而第一个无效。
解决方案
推荐阅读
- performance - 如何找到消耗高 CPU 和内存的 Jenkins 构建作业
- azure-application-insights - Azure 应用程序洞察力查询合并行
- c# - asp.net mvc(迁移)
- csv - 如何在 Paraview 中查看所有时间步长的总和/平均速度?
- javascript - 带有简单表单的网页在表单提交时重新加载
- java - 创建一个接受任何实体类列表并使用 apache Diff 检查差异的通用方法
- azure-devops - 如何在 Azure DevOps 中从“通过任务管理错误”切换到“根据要求管理错误”?
- c# - WPF MVVM 中 ViewModel 上的 Mediatr 通知
- c# - Microsoft Bot Framework V4 如何获取当前正在执行的对话框集 ID
- python - 检查项目是否在列表列表中