gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:49:48 24 4
gpt4 key购买 nike

以下是对 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

关于curl - 有效的 GitHub api v4 查询不断返回错误 "Problems parsing JSON",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45390076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com