如何使用 axios 方法获取请求。发送 uid、客户端和令牌

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

我正在尝试从 API 获取数据,但它需要我发送 ->(客户端 - uid 和令牌信息)。我如何使用 redux-sagas 和 axios 来做到这一点?但它适用于邮递员。

SAGAS.TS

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

    const resp = yield call(api.get, 'enterprises', {headers: {
      client: "nyzcig9rTLqAKeHLmoL4LQ",
      'access-token': "Ds23VSssxu2mDafOIHqmcg",
      uid: "testeapple@company.com.br"
    }});

    console.log(payload)

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

    );
  }
}

Return 错误:[错误:请求失败,状态代码为 401]

我设法解决了我的问题,我提出了错误的请求,按照代码:(我希望它能帮助别人)

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());
 }
}