question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to include headers in RESTDataSource request?

See original GitHub issue

I’m trying to use apollo RESTDataSource to wrap my rest api. I need to pass some headers to the api call.

I’m following the example from the docs: https://www.apollographql.com/docs/apollo-server/features/data-sources#intercepting-fetches

This is my code:

  willSendRequest(request: RequestOptions) {
    console.log(`request 1: ${JSON.stringify(request)}`);
    request.headers.set('Authorization', this.context.authorization);
    console.log(`request 2: ${JSON.stringify(request)}`);
  }

I’m expecting the headers to contain ‘Authorization’. But it’s always empty.

The log from the above code:

request 1: {"method":"POST","path":"partnerinvoices","body":{"command": "input","params":{},"headers":{}}
request 2: {"method":"POST","path":"partnerinvoices","body":{"command":"input","params":{},"headers":{}}

I can override body and params in willSendRequest method without any problem.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

19reactions
abernixcommented, Jul 16, 2019

The third option to, e.g. post, put, delete, etc. allows passing of fetch options. For example:

class MoviesAPI extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = 'https://movies-api.example.com/';
  }

  async getMovie(id) {
    return this.get(`movies/${id}`, undefined, {
      headers: {
        'Authorization': 'Bearer 123abc',
      },
    });
  }
}
3reactions
shandroliscommented, Apr 23, 2020

@abernix Thanks for your answer, my code worked after adding undefined to the GET request, but I’m confused that what is undefined in the syntax? Any documentation for reference? Thank you!

Second function param is to add (url)query parameters, so you don’t have to manually add it to the path yourself. Its usage and source can be seen here. I’m not sure if there are extensive official docs on it, but you can quite easily look in the source.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to include headers in RESTDataSource request?
You need to use request.headers.get('Authorization') to get your desired data. Using JSON.stringify will not give you the headers values as ...
Read more >
Using headers with Apollo Datasources | wehkamp-techblog
However, the solution for this is simple: get the headers from the request to /graphql and pass them to the DataSource request.
Read more >
Fetching from REST - Apollo GraphQL Docs
The request is a GET , and the response specifies caching headers (e.g., ... RESTDataSource includes convenience methods for common REST API request...
Read more >
apollo-datasource-rest - npm
Data sources allow you to intercept fetches to set headers, query parameters, or make other changes to the outgoing request. This is most...
Read more >
Headers - Telerik UI for ASP.NET MVC - Documentation
Learn how to set request headers in the DataSource component for ASP. ... example demonstrates how to use the Headers option to set...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found