Search code examples
reactjsgetaxioshttprequestredux-saga

How to request with method get with axios. Send uid, client and token


I'm trying to get data from an API, but it needs me to send -> (client - uid and token information). How do I do this using redux-sagas and axios? BUT it works on the postman.

enter image description here

SAGAS.TS

export function* getListTasks({payload}) {
  try {

    const resp = yield call(api.get, 'enterprises', {headers: {
      client: "nyzcig9rTLqAKeHLmoL4LQ",
      'access-token': "Ds23VSssxu2mDafOIHqmcg",
      uid: "[email protected]"
    }});

    console.log(payload)

    //yield put(setListCompanys(resp.data));
  } catch (err) {
    Alert.alert(
      'ERROR REUQEST',
      'ERROR: '+err,

    );
  }
}

Return Error: [Error: Request failed with status code 401]


Solution

  • I managed to solve my problem, I was making the request wrongly, follow the code: (I hope it helps someone)

    export function* signIn({payload}) {
      try {
       const {email, password} = payload;
    
       const response = yield call(api.post, 'users/auth/sign_in', {
       email,
       password,
     });
    
     const login = {
       token: response.headers['access-token'],
       uid: response.headers.uid,
       client: response.headers.client,
     };
    
     if (login) {
       api.defaults.headers.Authorization = `Bearer ${login.token}`;
       api.defaults.headers['access-token'] = login.token;
       api.defaults.headers['uid'] = login.uid;
       api.defaults.headers['client'] = login.client;
     }
    
     yield put(signInSuccess(login, response.data));
     } catch (err) {
       Alert.alert(
        'Falha na autenticação',
       'Houve um erro no login, verifique seus dados: ' + err,
     );
     yield put(signFailure());
     }
    }