Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[apollo-datasource-rest]: Cannot override Content-Type header #2035

Closed
osteel opened this issue Nov 27, 2018 · 7 comments
Closed

[apollo-datasource-rest]: Cannot override Content-Type header #2035

osteel opened this issue Nov 27, 2018 · 7 comments

Comments

@osteel
Copy link

osteel commented Nov 27, 2018

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

@aliasmrchips
Copy link

Any resolution/work-around for this?

@osteel
Copy link
Author

osteel commented Dec 20, 2018

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.

@aliasmrchips
Copy link

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.

@felippepuhle
Copy link

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.

@jlally21
Copy link
Contributor

jlally21 commented Mar 29, 2019

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.

@saernz
Copy link

saernz commented Apr 11, 2019

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)
    }

...

}

@abernix
Copy link
Member

abernix commented Jul 16, 2019

Fixed via #2520, in 2.6.0.

@abernix abernix closed this as completed Jul 16, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants