Search code examples
javascriptreactjslaravelreact-nativelaravel-passport

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


Solution

  • 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