首页 > 解决方案 > 从 json 文件批量 curl -d

问题描述

我有一个 json 文件,我想将每个对象传递给 curl -d 命令:

[
{
    "number":"+336770002979",
    "message":"La plupart\ntest",
    "sender":"BEcompany",
    "date": 1539286620000
},
{
    "number":"+336600000780",
    "message":"La plupart\ntest",
    "sender":"BEcompany",
    "date": 1539286620000
},
...
]

现在我试过这个

curl -X POST \
-H "X-Primotexto-ApiKey: 784155eed9d0a4d1ffdb67466" \
-H "Content-Type: application/json" \
-d @json.json \
https://api.primotexto.com/v2/notification/messages/send;

但它只读取第一个对象。

编辑[...]由于您的评论,我修复了json 和 curl 命令。

标签: jsonbash

解决方案


您可以使用jq -c .[]将文件拆分为每行一个数组元素,然后从那里获取它。

jq -c .[] json.json |
while IFS= read -r fragment; do
    curl -options -etc -d "$fragment" "http://url"
done

推荐阅读