【问题标题】:How to post multiple json from one file with cURL如何使用 cURL 从一个文件中发布多个 json
【发布时间】:2017-07-18 17:48:40
【问题描述】:

我在一个文件中存储了一些 json 数据。该文件的每一行都是一条 json 消息。

而不是: curl -X POST -d '{data:{something}}' 逐个。我尝试使用:

   while read data
   >do
   >curl -X POST -d @$data
   >done < file

但是它不起作用,因为 shell 可能会将数据是一些没有单引号的文本。

无论如何我可以修改这个脚本吗?

错误信息是这样的

curl: (3) [globbing] illegal character in range specification at pos 3

curl: (3) [globbing] illegal character in range specification at pos 3

* getaddrinfo(3) failed for "something"::10
* Couldn't resolve host '"something":'
* Closing connection #1
curl: (6) Couldn't resolve host '"something":'
curl: (3) [globbing] unmatched brace at pos 20

etc...

【问题讨论】:

  • Curl -d 选项正在尝试读取文件,因为它有 @。删除@,先试试吧。
  • @Azize 实际上我已经尝试过了,同样的错误。
  • 把错误信息放在这里,对我们有帮助。
  • @Azize 当然,但就像我所说的,错误消息就像你在curl -X POST -d {data:{something}} 而不是curl -X POST -d '{data:{something}}' 时得到的一样。没有单引号
  • 这样使用变量-d "${data}",所以会引用数据内容。但是你也需要通知地址来卷曲。确保你没有错过它。

标签: json bash shell curl


【解决方案1】:

您需要将shell变量双引号以防止shell执行word splittingfilename expansion

curl -X POST -d "$data"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-23
    • 2023-04-08
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 2011-03-31
    相关资源
    最近更新 更多