Apollo-server: [apollo-datasource-rest]: Cannot override Content-Type header

Created on 27 Nov 2018  路  7Comments  路  Source: apollographql/apollo-server

Hi guys,

I am trying to consume an API that follows the JSON:API specification, requiring the Content-Type header to be set to application/vnd.api+json.

I tried using willSendRequest in the resolver:

willSendRequest(request) {
    request.headers.set('Content-Type', 'application/vnd.api+json');
    request.headers.set('Accept', 'application/vnd.api+json');
}

While the Accept header is correctly set, the Content-Type one's value remains application/json.

This is because of the fetch method of RESTDataSource.js, which overwrites the Content-Type header after willSendRequest is called (line 140):

if (options.body !== undefined &&
    options.body !== null &&
    (options.body.constructor === Object ||
        (options.body.toJSON &&
            typeof options.body.toJSON === 'function'))) {
    options.body = JSON.stringify(options.body);
    options.headers.set('Content-Type', 'application/json');
}

I think this header should only be overwritten when it is not already set.

Thanks,

Yannick

Most helpful comment

All 7 comments

Any resolution/work-around for this?

Afraid not. Luckily on my end we've got some control over the API we consume so we managed to get application/json accepted as well for Content-Type, but I can't think of a workaround for APIs which expect another header value. And AFAIK we can't submit PRs for the apollo-datasource-rest package.

Too bad. I also have control over the API, but I would have preferred changing the Content-Type header. This seems like a bug to me.

I'm experiencing this too... I think we should verify if Content-Type wasn't previously set here.
I can send a PR if you guys think that makes sense.

Experiencing this issue too...any suggestions on a workaround? Besides controlling at the server API as @aliasmrchips explained.

Edit: I have used npm module patch-package as a workaround for now, commenting out line 141 in RESTDataSource.js.
Will try to make a PR over the weekend.

Another work around is to just straight use node-fetch for now which is a dependency of apollo server, it's kinda ugly but looks like you've got a PR not far away from being merged.

const fetch = require('node-fetch')

...

class ExampleAPI extends RESTDataSource {

...

    async createSomething(someArgs) {
        const data = {...}
        const res = await fetch(`${this.baseURL}/example`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/vnd.api+json',
                'Accept': 'application/vnd.api+json',
            },
            body: JSON.stringify(data)
        }).then(response => response.json())

        return this.exampleReducer(res.data)
    }

...

}
Was this page helpful?
0 / 5 - 0 ratings