Ah Dee
Ah Dee

Reputation: 31

React native able to GET with access_token but unable to POST, it return "Unauthenticated."

I using Axios GET with a pass in header access_token to Laravel api, it worked fine. After I created another method that using POST, and I have created Axios POST with a pass in header access_token, but it returns error "Unauthenticated.". May I know how to fix this problem? Thank you.

I have tried to change GET to become POST in the same method, while GET is successfully but POST is still returned error "Unauthenticated.".

 getEmployeeClockInPosition = async () => {
      let GET_EMPLOYEE_COMPANY_POSITION = 'http://localhost/get-employee-company-location';
      await axios.post(GET_EMPLOYEE_COMPANY_POSITION, {
        headers: {
          'Content-Type' : 'application/json',
          'Authorization' : 'Bearer '+ this.props.authState.accessToken,
          'Accept' :'application/json',
        },
        userID: this.props.authState.id,
      })
      .then(res => {
        console.log(res.data);
      })
      .catch(function(error) {
        console.log(error);
      });
    }
Route::group(['middleware' => 'auth:api'], function() {
        Route::post('/get-employee-company-location','Clocking\DashboardController@getEmployeeCompanyLocation');
});

Upvotes: 1

Views: 208

Answers (1)

user3764429
user3764429

Reputation: 402

According to the official documentation, the parameters of axios.post should go in this order:

axios.post(url[, data[, config]])

In your case, you provide post config before the data.

Please try this approach:

let postData = {
  userID: this.props.authState.id
}

let config = {
  headers: {
    'Content-Type' : 'application/json',
    'Authorization' : 'Bearer '+ this.props.authState.accessToken,
    'Accept' :'application/json'
  }
}

axios.post(GET_EMPLOYEE_COMPANY_POSITION, postData, config)

Upvotes: 1

Related Questions