4

编辑:下面描述的问题是我使用的api的问题......不是阿波罗的问题。

我正在尝试apollo-link-rest与标题中设置的 api-key 一起使用。如果我尝试使用完全相同的 api 端点并将 api-key 设置为 Postman 应用程序中的标头,它工作得非常好。但不适用于apollo-link-rest

import { AppContainer } from 'react-hot-loader';
import React from 'react';
import ReactDOM from 'react-dom';
import App from './react-components/App';
import PropTypes from 'prop-types';
import { RestLink } from "apollo-link-rest";
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloProvider } from 'react-apollo';
import 'normalize.css/normalize.css';
import './index.css';

const restLink = new RestLink({
    uri: 'https://a-site.net/api',
    headers: {
        'x-someapikey': '012737465334232522z2z22163r43'
    },
});

const client = new ApolloClient({
  link: restLink,
  cache: new InMemoryCache(),
});

我做错了什么?

这就是在 Chrome Devtools 中打印的请求标头:

在 Chrome Devtools 中打印的请求标头

我在浏览器(Apollo)中收到 401 错误...但是当我在 Postman 应用程序中使用具有相同标头的相同端点时...它可以工作!

4

2 回答 2

1

这是 API 的问题……不是 Apollo 的问题。

于 2018-10-16T20:12:26.540 回答
0

你能试试这个吗?我的回答基于此:https ://www.apollographql.com/docs/link/links/rest.html#context

var myHeaders = new Headers();
myHeaders.append('x-someapikey', '012737465334232522z2z22163r43');

const restLink = new RestLink({
    uri: 'https://a-site.net/api',
    headers: myHeaders
});
于 2018-10-13T15:48:23.103 回答