2

I'm trying to send a http request to Githubs graphql api(v4). My guess is that the format of my query is wrong. The code below is used to send a POST request to the api.

app.get('/fetch-data', function(req, res, next) {
    const access_token = settings.dev.TOKEN;
    const query = {query: { viewer: { 'login': 'muckbuck' } }};

    const options = {
        uri: 'https://api.github.com/graphql',
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Authorization': "Bearer " + access_token,
            'User-Agent': 'request',
            'contentType': "application/graphql",
        },
        data: query
    };

    function callback(error, response, body) {
        console.log(response.body)
        if (!error && response.statusCode == 200) {
            console.log(body);
            console.log('lol');
        }
    }
    request(options, callback);



});

The error message that I get:

{"message":"Problems parsing JSON","documentation_url":"https://developer.github.com/v3"}
2
  • Sure about "contentType"? Shouldn't that be "content-type"??? Commented Aug 8, 2017 at 13:35
  • Yes I think you are right but sadly it does not solve the problem. Commented Aug 8, 2017 at 13:44

1 Answer 1

1

I believe your GraphQL is malformed in that the query key is supposed to contain a string, not a complex structure. See also this example from the GitHub API documentation.

You may also want to set Content-Type to application/json as is recommended by the GraphQL introduction. application/graphql does not seem to be a registered media type and appears to alter the behaviour of GraphQL.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I changed the query to: { "query": "query:{viewer:{login}}" };. I also changed the Content-Type to application/json but it's still not parsing correctly :/. There was also a note about escaping some characters in the link, don't know how to do that, will have to google it I guess.
Try with { "query": "query { viewer { login } }" }. GraphQL isn't JSON. Again, check the example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.