Here is an example of a cURL query to the GitHub api v4 that keeps returning an error:
curl -H "Authorization: bearer token" -X POST -d " \
{ \
\"query\": \"query { repositoryOwner(login: \"brianzelip\") { id } }\" \
} \
" https:\/\/api.github.com\/graphql
The error that is returned:
{
"message": "Problems parsing JSON",
"documentation_url": "https://developer.github.com/v3"
}
Why do I keep getting this error?
According to the GH api v4 docs about forming query calls, the above cURL command is valid. Here's what the docs say that backs up my claim that the above cURL command is valid:
curl -H "Authorization: bearer token" -X POST -d " \
{ \
\"query\": \"query { viewer { login }}\" \
} \
" https://api.github.com/graphql
Note: The string value of "query" must escape newline characters or the schema will not parse it correctly. For the POST body, use outer double quotes and escaped inner double quotes.
When I enter the above query into the GitHub GraphQL API Explorer, I get the expected result. The format of the above cURL command looks like this for the GH GraphQL Explorer:
{
repositoryOwner(login: "brianzelip") {
id
}
}
You have to escape nested double quotes in query
JSON field, your actual body would be :
{
"query": "query { repositoryOwner(login: \"brianzelip\") { id } }"
}
So replace \"brianzelip\"
with \\\"brianzelip\\\"
:
curl -H "Authorization: bearer token" -d " \
{ \
\"query\": \"query { repositoryOwner(login: \\\"brianzelip\\\") { id } }\" \
} \
" https://api.github.com/graphql
You can also use single quotes instead of double quotes to wrap the body :
curl -H "Authorization: bearer token" -d '
{
"query": "query { repositoryOwner(login: \"brianzelip\") { id } }"
}
' https://api.github.com/graphql
You could also use heredoc :
curl -H "Authorization: bearer token" -d @- https://api.github.com/graphql <<EOF
{
"query": "query { repositoryOwner(login: \"brianzelip\") { id } }"
}
EOF
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With