appolo-link-rest set header

1.1k Views Asked by At

EDIT: The problem described below is a problem of the api i used... not a problem with apollo.

I am trying to use apollo-link-rest with a api-key set in the header. If I try to use the exact same api endpoint and set the api-key as header in Postman app, it works perfectly fine. But not for 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:

Request Header printed in 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!

2

There are 2 best solutions below

0
christian wuensche On BEST ANSWER

It was a problem with the API... not with Apollo.

0
Lukasz On

Can you try this? I kind of based my answer on this: 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
});