【问题标题】:Sending large file as data inside JSON via cURL通过 cURL 将大文件作为 JSON 中的数据发送
【发布时间】:2019-08-27 17:04:22
【问题描述】:

我正在尝试通过 POST 将文件作为 base64 编码数据发送到 Bugzilla REST API,如下所示:

curl -X POST https://www.example.com/rest/bug/$id/attachment -H "Content-Type: application/json" \
-d "{\
\"login\" : \"$username\", \
\"password\" : \"$password\", \
\"ids\" : [ $id ], \
\"summary\" : \"...\", \
\"content_type\" : \"application/gzip\", \
\"data\" : \"$data\"\
}"

我得到的是来自 cURL 的错误,即参数列表太长。据推测,这是因为我尝试发送的文件 ($data) 超过了 shell 的最大值(文件为 11M)。我在网上看到的是,解决这个问题的最好方法是让 cURL 使用--data-binary 从文件中读取数据。但由于我需要发送用户名和密码,我宁愿不必将整个文件与它们一起保存在里面。

有没有办法绕过这个最大值,或者有没有其他方法可以通过这种方式发送大量数据?我更喜欢原生 Linux 工具,因为我希望这个脚本是可移植的。

【问题讨论】:

    标签: bash curl


    【解决方案1】:

    您可以尝试使用这样的文件,这是推荐的方式。

    curl -i \
        -H 'Accept:application/json' \
        -H 'Authorization:Basic $username:$password' \
        -X POST -d @datafile.txt https://www.example.com/rest/bug/$id/attachment
    

    【讨论】:

    • 我认为 Bugzilla 不支持授权头,而且看起来不支持。当我尝试使用它时,我回来了{"documentation":"https://www.example.com/docs/en/html/api/index.html","error":true,"code":410,"message":"You must log in before using this part of ...."}。 Bugzilla 文档中没有 410 错误,并且当授权无效而不是不存在时会出现错误。
    猜你喜欢
    • 1970-01-01
    • 2016-07-03
    • 2019-04-15
    • 2011-10-24
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多