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

Change default Accept header to application/json #9309

Closed

Conversation

sambostock
Copy link

@sambostock sambostock commented Jan 14, 2022

What

While the GraphQL spec does not specify JSON as the serialization format, it it by far the most popular.

Therefore, it we can provide a better default accept header, especially given we already default to application/json for the content-type header. Consumers continue to have the ability to override this if necessary.

This changes the default accept header from */* to application/json.

Why

Setting reasonable defaults can help consumers who might not think to set certain configuration, or who assume it is already being set.

One example of how this particular setting is useful is for consumers using an API behind an authorization reverse proxy (such as OAuth2-Proxy) which checks if the request is authorized before forwarding it to the actual GraphQL server. Many such services will check the Accept header to determine the appropriate response for an unauthorized request. For example, OAuth2-Proxy's behaviour is:

If authentication is required but missing then the user is asked to log in and redirected to the authentication provider (unless it is an Ajax request, i.e. one with Accept: application/json, in which case 401 Unauthorized is returned)

Currently, Apollo-Client uses accept: */*, which means that unauthorized requests are routed to the authentication provider instead of resulting in a 401 Unauthorized response. Apollo-Client then follows the redirect resulting in it trying to handle a 200 OK response containing an HTML login page, which blows up on JSON.parse.

By setting a better default, these cases will result in a far more easily actioned error.

Checklist:

  • If this PR is a new feature, please reference an issue where a consensus about the design was reached (not necessary for small changes)
  • Make sure all of the significant new logic is covered by tests

@apollo-cla
Copy link

@sambostock: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

@@ -95,7 +95,7 @@ const defaultHttpOptions: HttpQueryOptions = {

const defaultHeaders = {
// headers are case insensitive (https://stackoverflow.com/a/5259004)
accept: '*/*',
accept: 'application/json',
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be documented somewhere (in addition to in CHANGELOG).

It is probably worth considering if there are other sane defaults we should set here.

@sambostock
Copy link
Author

CI failures seem unrelated

Error: Cannot find module 'webpack/lib/RequestShortener'

@sztadii
Copy link
Contributor

sztadii commented Jan 15, 2022

CI failures seem unrelated

Error: Cannot find module 'webpack/lib/RequestShortener'

Yes, it is related to this #9287.
I solve the problem by creating the branch from the previous commit.

While the GraphQL spec does not specify JSON as the serialization format, it it
by far the most popular. Therefore, it we can provide a better default accept
header, especially given we already default to "application/json" for the
content-type header. Consumers continue to have the ability to override this if
necessary.
@sambostock sambostock force-pushed the improve-default-headers branch from cec3787 to c8f6903 Compare January 20, 2022 22:01
@jpvajda jpvajda added 🏓 awaiting-contributor-response requires input from a contributor 🏓 awaiting-team-response requires input from the apollo team and removed 🏓 awaiting-contributor-response requires input from a contributor labels May 3, 2022
@jerelmiller
Copy link
Member

Hey @sambostock 👋 I'm going through some older PRs and came across this one.

While I think this change is a positive one for the reasons you mention in the description, unfortunately this might be a breaking change for some users. I've added this to our 4.0 wishlist of things we're considering changing.

In the meantime, I'm going to go ahead and close this. We'll revisit this when we get closer to development on 4.0. Appreciate the PR and thought put behind it!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🏓 awaiting-team-response requires input from the apollo team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants