How to use curl to access the github graphql API

2019-03-27 15:03发布

After referring this guide I needed to access the github graphql by using curl for a testing purpose. 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

problems parsing JSON

what I am doing wrong. I spent nearly 2 hours trying to figure it and tried different examples but none of them worked. Can you please be kind enough help me resolve this

2条回答
beautiful°
2楼-- · 2019-03-27 15:19

You just need to escape the double quotes that are inside the JSON as the query

$ 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

查看更多
放荡不羁爱自由
3楼-- · 2019-03-27 15:20

If you want your queries to stay nice and multiline, you may do like 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
查看更多
登录 后发表回答