简体   繁体   中英

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:

     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

The correct way to set headers in Axios Wrong-way:

     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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM