有效 GitHub api v4 查询不断返回错误 "Problems parsing JSON"

valid GitHub api v4 query keeps returning error "Problems parsing JSON"

下面是对 GitHub api v4 不断返回错误的 cURL 查询示例:

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命令有效。以下是支持我关于上述 cURL 命令有效声明的文档所说的内容:

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 Explorer,上述 cURL 命令的格式如下所示:

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

您必须在 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