How to include headers in RESTDataSource request?
See original GitHub issueI’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:
- Created 4 years ago
- Comments:9 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The third option to, e.g.
post
,put
,delete
, etc. allows passing offetch
options. For example: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.