使用curl时Github API错误“解析JSON时出现问题”但与Postman一起工​​作正常

J. *_*Doe 2 json curl github github-api

我正在尝试使用 Github 的 API 创建一个版本。

我的请求在 Postman 中工作正常,但无论我尝试什么,curl 总是失败,包括如果我只是使用 Postman 将 Postman 请求转换为curl。

这是我的 Postman POST 请求的正文:

{
    "tag_name": "4.2.0",
    "target_commitish": "master",
    "name": "4.2.0",
    "body": "test"
}
Run Code Online (Sandbox Code Playgroud)

我已经包含了一个“Basic”类型的授权标头,我在其中输入我的用户名和为此目的创建的令牌。
我正在执行针对 的请求https://api.github.com/repos/<myUsername>/<myRepo>/releases

正如我所说 - 它工作正常,但是当我将其转换为 curl 时,我收到错误“解析 JSON 时出现问题”。

翻译后的curl命令是:

curl --location --request POST 'https://api.github.com/repos/<myUsername>/<myRepo>/releases' \
--header 'Authorization: Basic <someHashOrSomething>' \
--header 'Content-Type: application/json' \
--data-raw '{
    "tag_name": "4.2.0",
    "target_commitish": "master",
    "name": "4.2.0",
    "body": "test"
}'
Run Code Online (Sandbox Code Playgroud)

我将其重新格式化curl --location --request POST 'https://api.github.com/repos/<myUsername>/<myRepo>/releases' --header 'Authorization: Basic <someHashOrSomething>' --header 'Content-Type: application/json' --data-raw '{ "tag_name": "4.2.0", "target_commitish": "master", "name": "4.2.0", "body": "test"}'为一行。

我也尝试过(因为只需要“tag_name”参数):
curl -i -H 'Authorization: token <myToken>' -d '{"tag_name":"4.2.0"}' https://api.github.com/repos/<myUsername>/<myRepo>/releases
curl -i -H 'Authorization: token <myToken>' -d '{"tag_name":"4.2.0"}' https://api.github.com/repos/<myUsername>/<myRepo>/releases --header Content-Type:application/json
curl -d '{"tag_name":"4.2.0"}' -u <myUsername>:<myToken> https://api.github.com/repos/<myUsername>/<myRepo>/releases --header "Content-Type:application/json"
curl -d "tag_name=4.2.0" -u <myUsername>:<myToken> https://api.github.com/repos/<myUsername>/<myRepo>/releases --header "Content-Type:application/json"

每个curl请求都会失败,并出现“解析JSON时出现问题”错误。

小智 6

如果您是从 Windows cmd 提示符执行此操作,则需要更改

--data-raw '{   "tag_name": "4.2.0",    "target_commitish": "master",   "name": "4.2.0",    "body": "test"}'
Run Code Online (Sandbox Code Playgroud)

--data-raw "{   \"tag_name\": \"4.2.0\",    \"target_commitish\": \"master\",   \"name\": \"4.2.0\",    \"body\": \"test\"}"
Run Code Online (Sandbox Code Playgroud)

cmd 提示符似乎不喜欢数据的单引号。