2017-07-29 68 views
1

这里是卷曲查询GitHub的API V4,保持返回一个错误的例子:返回的有效GitHub的API v4用户查询保留返回错误“问题解析JSON”

curl -H "Authorization: bearer token" -X POST -d " \ 
{ \ 
    \"query\": \"query { repositoryOwner(login: \"brianzelip\") { id } }\" \ 
} \ 
" https:\/\/api.github.com\/graphql 

错误:

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

为什么我不断收到此错误?


根据该GH api v4 docs about forming query calls,上述卷曲的命令是有效的。下面是该文档说备份我的要求,上述卷曲的命令是有效的:

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.

当我输入上面的查询到GitHub GraphQL API Explorer,我得到预期的结果。上述卷曲命令的格式看起来像这样的GH GraphQL资源管理器:

{ 
    repositoryOwner(login: "brianzelip") { 
    id 
    } 
} 
+0

是不是真棒如何GraphQL探索正确地解析JSON,但使用使用完全相同的查询API时,它的问题。 – bart

回答

1

你要逃避query JSON场的嵌套双引号,你实际的身体将是:

{ 
"query": "query { repositoryOwner(login: \"brianzelip\") { id } }" 
} 

所以更换\"brianzelip\"\\\"brianzelip\\\"

curl -H "Authorization: bearer token" -d " \ 
{ \ 
    \"query\": \"query { repositoryOwner(login: \\\"brianzelip\\\") { id } }\" \ 
} \ 
" https://api.github.com/graphql 

您还可以使用单引号代替双引号来包裹身体:

curl -H "Authorization: bearer token" -d ' 
{ 
    "query": "query { repositoryOwner(login: \"brianzelip\") { id } }" 
} 
' https://api.github.com/graphql 

您还可以使用heredoc

curl -H "Authorization: bearer token" -d @- https://api.github.com/graphql <<EOF 
{ 
    "query": "query { repositoryOwner(login: \"brianzelip\") { id } }" 
} 
EOF