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

Request payload not compatible with Github API #21

Closed
fabiomolinar opened this issue Jan 20, 2018 · 3 comments
Closed

Request payload not compatible with Github API #21

fabiomolinar opened this issue Jan 20, 2018 · 3 comments

Comments

@fabiomolinar
Copy link

fabiomolinar commented Jan 20, 2018

I have tried using this repository to query Github's GraphQL API. But no matter what I do, I keep getting a "Problems parsing JSON" message.
An extracted piece of the code below:

var graph = graphql("https://api.github.com/graphql", {
  method: "POST",
  headers: {
    "Authorization": "Bearer <my-token-here>",
    "Content-Type": "application/json"
  },
  fragments: {
    rateLimitInfo: "on RateLimit {cost,remaining,resetAt}"
  }
});
graph(`
    query repo($name: String!, $owner: String!){
        repository(name:$name, owner:$owner){
            id      
        }
    }
`,{
    name: "freeCodeCamp",
    owner: "freeCodeCamp"
}).then(function(response){
    console.log(response);
}).catch(function(error){
    console.log(error);
});

With this code I always get a HTTP 400 (Bad Request) response. With the following data:

{
    message: "Problems parsing JSON", 
    documentation_url: "https://developer.github.com/v3"
}

Looking at Chrome's Network tab, I see that the request payload that is being sent is:
query=query%20repo(%24name%3A%20String!%2C%20%24owner%3A%20String!)%7Brepository(name%3A%24name%2C%20owner%3A%24owner)%7Bid%7D%7D&variables=%7B%22name%22%3A%22freeCodeCamp%22%2C%22owner%22%3A%22freeCodeCamp%22%7D
I have the same query running on Insomnia and it works. When I convert Insomnia's request to curl, the payload looks like this:
--data '{"query":"query repo($name: String!, $owner: String!){\n\trepository(name:$name, owner:$owner){\n\t\tid\t\t\n\t}\n}","variables":{"name":"freeCodeCamp","owner":"freeCodeCamp"},"operationName":"repo"}'

When decoding both strings, it is visible that both payloads have different formats.

Is this an issue with how the repository is creating the payload, is this a mistake on my code, or is this something particular to how Github's API accept the query string?

Same question posted on stackoverflow.

@f
Copy link
Owner

f commented Jan 22, 2018

Maybe I can implement an option how to send the data.

@bwyss
Copy link

bwyss commented Jun 27, 2018

I have the same issue, any progress on this?

@f
Copy link
Owner

f commented Jun 27, 2018

You may need to pass asJSON: true option since default content-type is application/form-data but GH API wants it as application/json.

https://github.com/f/graphql.js#change-post-method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants