我的获取路线有什么问题?当我尝试运行Get路线时,我会收到401状态代码
我的获取路线是返回401个未经授权的状态代码,但不确定,但一切都在邮递员中起作用,因此我假设这是在我的反应中孤立的,而不是我的API问题。使用携带者令牌进行AUTH,并在需要时可以提供更多的auth代码。使用mern堆栈
我客户端路由的API文件中获取路由 -
export const getOneApplication = (user, applicationId) => {
return axios({
url: `${apiUrl}/application/${applicationId}`,
method: 'GET',
headers: {
Authorization: `Token token=${user.token}`
}
})
}
从app.js的
<Route
path='/application/:id'
element={
<ShowApplication user={user} msgAlert={msgAlert}/>}
/>
从后端获取路由
router.get('/application/:id', requireToken, (req, res, next) => {
// req.params.id will be set based on the `:id` in the route
Application.findById(req.params.id)
.then(handle404)
// if `findById` is succesful, respond with 200 and "application" JSON
.then((application) => res.status(200).json({ application: application.toObject() }))
// if an error occurs, pass it to the handler
.catch(next)
})
my get route is returning a 401 unauthorized status code and undefined but everything works in postman so I'm assuming that this is isolated in my react and not a problem with my api. Using a bearer token for auth and can provide more of the auth code if needed. using mern stack
get route in the api file of my client-
export const getOneApplication = (user, applicationId) => {
return axios({
url: `${apiUrl}/application/${applicationId}`,
method: 'GET',
headers: {
Authorization: `Token token=${user.token}`
}
})
}
route from app.js-
<Route
path='/application/:id'
element={
<ShowApplication user={user} msgAlert={msgAlert}/>}
/>
get route from backend api-
router.get('/application/:id', requireToken, (req, res, next) => {
// req.params.id will be set based on the `:id` in the route
Application.findById(req.params.id)
.then(handle404)
// if `findById` is succesful, respond with 200 and "application" JSON
.then((application) => res.status(200).json({ application: application.toObject() }))
// if an error occurs, pass it to the handler
.catch(next)
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案最终是我没有将用户传递到组件中的使用效应,在运行路线时,这会导致我丢失令牌并获取401状态代码
The answer ended up being that I was not passing the user into the useEffect in the component, which when running the route caused me to lose the token and get the 401 status code