【问题标题】:Troubles in Expanding Expression within a JSON Object在 JSON 对象中扩展表达式的问题
【发布时间】:2018-12-24 19:54:17
【问题描述】:

我正在尝试使用 GitHub API 创建问题,作者:

curl -u $username  -d '{"title" : "Big Files List" , "body" : "'$(find -type f -size +1M)'", "label" : "big files" } $URL -k'

但是,我得到了像

这样的响应
curl: (3) [globbing] unmatched close brace/bracket at pos 56
{
"message": "Invalid request.\n\nFor 'links/0/schema', \"body\" is not an 
object.",
"documentation_url": 
"https://developer.github.com/enterprise/2.15/v3/issues/#create-an-issue"
}

所以问题在 $(find -type f -size +1M) 之内,当我用字符串替换时,没有问题。

【问题讨论】:

  • find 正在返回多个文件名,因此 -d 参数被拆分为多个参数。

标签: linux shell unix github-api github-api-v3


【解决方案1】:

curl 返回多个文件名,第一个文件名后面的空格结束了 -d 参数,因此您发送的 JSON 不完整。你需要引用它,这样它就不会被拆分。

但这还不够,因为 JSON 中也不允许使用文字换行符。您需要将换行符翻译成\n

你也把结束引号放在了错误的地方,它应该在 JSON 的末尾,而不是行尾。

bigfiles=$(find -type f -size +1M)
bigfiles=${bigfiles// /\\n}
curl -u $username  -d '{"title" : "Big Files List" , "body" : "'"$bigfiles"'", "label" : "big files" }' $URL -k

【讨论】:

    猜你喜欢
    • 2010-11-06
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多