How to use curl to access github graphql API

After graphql with this tutorial, I needed to access github graphql using curl for testing. I tried this simple command

 curl -i -H "Authorization: bearer myGithubAccessToken" -X POST -d '{"query": "query {repository(owner: "wso2", name: "product-is") {description}}"}' https://api.github.com/graphql 

but it gives me

JSON parsing problems

what am I doing wrong I spent almost 2 hours trying to figure it out and tried different examples, but none of them worked. Please kindly help me solve this problem.

+5
github curl graphql
Feb 03 '17 at 9:38 on
source share
2 answers

You just need to avoid the double quotes that are inside JSON as a request

 $ curl -i -H 'Content-Type: application/json' -H "Authorization: bearer myGithubAccessToken" -X POST -d '{"query": "query {repository(owner: \"wso2\", name: \"product-is\") {description}}"}' https://api.github.com/graphql 
+6
Feb 03 '17 at 9:52
source share

If you want your queries to remain beautiful and multi-line, you can do this:

 script='query { repositoryOwner(login:\"danbst\") { repositories(first: 100) { edges { node { nameWithOwner pullRequests(last: 100, states: OPEN) { edges { node { title url author { login } labels(first: 20) { edges { node { name } } } } } } } } } } }' script="$(echo $script)" # the query should be onliner, without newlines curl -i -H 'Content-Type: application/json' \ -H "Authorization: bearer ........." \ -X POST -d "{ \"query\": \"$script\"}" https://api.github.com/graphql 
0
Aug 17 '18 at 12:21
source share



All Articles