简体   繁体   English

Laravel Passport 在 PostMan 上工作正常,但在反应原生应用程序中返回 401?

[英]Laravel Passport works fine on PostMan, but returns 401 inside the react native app?

When I make my Axios call inside of the react native app, it returns 401. However, when I grab the parsed header inside of the Network tab it shows:当我在 react native 应用程序内部进行 Axios 调用时,它返回 401。但是,当我在 Network 选项卡内获取已解析的 header 时,它显示:

     const config = { 
        method: "POST",
        baseURL: API_URL, 
        data:request_data,
        headers: { 
             'Authorization' : 'Bearer '+ token
           }
        }


  let  data  = await axios('user_update_profile',config)
  console.log(data)  //return 401 Unauthorized Error

Any idea how to fix this?知道如何解决这个问题吗? I am using Laravel version 6我正在使用 Laravel 版本 6

The correct way to set headers in Axios Wrong-way:在 Axios 中设置标头的正确方法 错误方式:

     const config = { 
        method: "POST",
        baseURL: API_URL, 
        data:request_data,
        headers: { 
             'Authorization' : 'Bearer '+ token
           }
        }


  let  data  = await axios('user_update_profile',config)

So the Correct way is所以正确的方法是

  let  data  = await axios.post(API_URL+'user_update_profile',request_data,{
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer '+token
  }
  })
  // console.log('----cheers---------',data)
dispatch(userUpdateProfileSuccess(data))

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Postman 中的 API 工作正常,但在本机反应中出现 422 错误 - API in Postman works fine but getting 422 error in react native API 在 postman 中工作正常,但在反应的前端部分中却没有 - API works fine in postman but not in frontend part of react 发布请求从本地主机返回 401 但从邮递员工作 - Post request returns 401 from localhost but works from postman 获取反应应用程序中的调用不返回正文,相同的调用适用于 postman - fetch call in react app doest not return body, same call works fine with postman 当我使用app.use时,Passport始终返回401 - Passport Always returns 401 when I use app.use Axios帖子在React Native上不起作用,但在PostMan上起作用 - Axios post not working on React Native but works on PostMan AWS API网关返回400错误请求,但Postman正常工作 - AWS API gateway returns 400 Bad Request, but Postman works fine React Typescript prop 工作正常,但返回错误 - React Typescript prop works fine, but returns error React 应用程序中的 Axios 请求响应 401,但在 Postman 中工作? - Axios request response 401 in React app, but working in Postman? 当我通过react应用发出请求时,我的网络服务器会发送404,但是当我直接通过chrome或postman访问它时,我的服务器工作正常 - My webserver sends 404 when i make the request through react app, but works fine when I access it directly through chrome or postman
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM