在api调用Redux Saga的标头中添加令牌

问题描述 投票:0回答:1

如何使用redux saga在api调用的标头中添加令牌?

const response = yield call(post, url, data.payload);

reactjs api redux token redux-saga
1个回答
0
投票

第一个arg post是您的api调用函数,redux-saga只是执行此函数并获得响应,因此您应将令牌添加到您的api调用函数中,而不是redux-saga。

您使用的是哪个api util?这两个是比较流行的api调用库。

  1. 使用获取
fetch(
  'url',
  {
    ...
    headers: {
      'Authorization': 'Bearer '+ /* your token string */
    }
  }
);
  1. 使用axios
import axios from 'axios';

export const api = axios.create({
    baseURL: process.env.REACT_APP_IP,
    timeout: 60000,
    headers: {'Authorization': 'Bearer '+ /* your token string */}
});
© www.soinside.com 2019 - 2024. All rights reserved.