我正在尝试使用终端创建别名来创建 GitHub 存储库,执行此操作的命令是
curl -u 'username' https://api.github.com/user/repos -d '{"name":"repo_name"}'
我在 .zshrc 文件 gitcreate() 中定义了一个函数,该函数应该使用第一个参数作为名称来创建 GitHub 存储库
gitcreate() {
curl -u 'my_gh_username' https://api.github.com/user/repos -d '{"name":"$1"}'
}
不幸的是,我不断收到错误:
{
"message": "Problems parsing JSON",
"documentation_url": "https://docs.github.com/rest/reference/repos#create-a-repository-for-the-authenticated-user"
}
我尝试逃避我在网上阅读的 $ 和其他建议,但没有任何效果,我希望有人对此有一个简单的解决方案/可以将我推荐到我可能能够找到答案的地方。谢谢!
只是
"{\"name\":\"$1\"}"
或
'{"name":"'"$1"'"}'
但最好是使用适当的 JSON 工具:
data=$(jq -n --arg arg "$1" '{"name": $arg}')
curl ... "$data"