【问题标题】:How do I put a header in an api request in fact redux-saga?实际上redux-saga如何在api请求中放置标头?
【发布时间】:2020-08-13 00:08:27
【问题描述】:

我想在 mypage api 请求的标头中放置一个令牌。
我在请求 api 时使用了一个名为 createRequestSaga 的函数。

import { call, put } from 'redux-saga/effects';
import { startLoading, finishLoading } from '../modules/loading';

export default function createRequestSaga(type, request) {
    const SUCECSS = `${type}_SUCCESS`;
    const FAILURE = `${type}_FAILURE`;

    return function* (action) {
        yield put(startLoading(type));  // START LOADING
        try {
            const response = yield call(request, action.payload);
            yield put({
                type: SUCECSS,
                payload: response.data,
            });
        } catch (e) {
            yield put({
                type: FAILURE,
                payload: e,
                error: true,
            });
        }
        yield put(finishLoading(type)); // FINISH LOADING
    };
}

这是 api 规范

在我的容器中,我知道如何在请求正文中发送变量, 像这样: dispatch(registerPost({ id, password, 昵称, email, team })); 但我不知道如何在标头中发送令牌。

【问题讨论】:

  • 我认为这应该是request 函数而不是redux-saga的责任。 Redux-saga just 调用这个网络函数。网络功能应在标头中添加令牌。您使用什么库来处理您的请求?爱讯?超级代理?

标签: reactjs react-redux redux-saga


【解决方案1】:
const result = yield call(
    rsf.functions.call,
    'sayHello',
    {
      name: 'Elon'
    },
    {
      method: 'POST',
      headers: {
        'Authorization': 'Bearer abc123'
      }
    }
  );

这对我有用。

参考-https://redux-saga-firebase.js.org/reference/dev/functions

【讨论】:

    猜你喜欢
    • 2016-06-09
    • 2020-03-12
    • 2017-10-31
    • 2020-07-03
    • 2017-02-25
    • 2018-03-16
    • 2020-10-04
    • 1970-01-01
    • 2017-05-21
    相关资源
    最近更新 更多