简体   繁体   English

appolo-link-rest设置头

[英]appolo-link-rest set header

EDIT: The problem described below is a problem of the api i used... not a problem with apollo. 编辑:下面描述的问题是我使用的api的问题...不是阿波罗的问题。

I am trying to use apollo-link-rest with a api-key set in the header. 我正在尝试将apollo-link-rest与标头中设置的api-key一起使用。 If I try to use the exact same api endpoint and set the api-key as header in Postman app, it works perfectly fine. 如果我尝试使用完全相同的api端点并将api-key设置为Postman应用程序中的标头,则可以正常工作。 But not for apollo-link-rest : 但不适用于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(),
});

What I am doing wrong? 我做错了什么?

Thats the Request Header printed in Chrome Devtools: 多数民众赞成在Chrome Devtools中打印的请求标头:

在Chrome Devtools中打印请求标题

I get a 401 Error in Browser (Apollo)... But when I use the same endpoint with the same header in Postman app... it works! 我在浏览器(Apollo)中收到401错误...但是当我在Postman应用程序中使用具有相同标头的相同终结点时,它起作用了!

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

Can you try this? 你可以试试这个吗? I kind of based my answer on this: https://www.apollographql.com/docs/link/links/rest.html#context 我的回答基于此: 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
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM