cURL : 在 POST 中从标准输入发送零件数据



我必须制作 2 个 cURL。来自 1 个 curl 的数据需要格式化并作为第二个 cURL 中的输入发送。我尝试了以下方法:

curl -XPOST -H "Content-type: application/json" -d '{"a":1, "b" : 2}' 'https://a.com/a' | Python -c "import json,sys;o=

json.load(sys.stdin(;o1=dict(id=o['foo'],hash=o['bar'](;p rint(json.dumps(o((;p rint(json.dumps(o1((;" | curl -XPOST -H "Content-type: application/json" https://a.com/b -d @-

在上面的 cURL 组合中,我通过 python 解析从第一个 cURL 接收的数据,然后在 stdin 上打印收到的输出和解析后的输出,然后将来自 stdin 的输入作为第二个 cURL 的数据。

问题:有两个 JSON 正在 stdin 中打印,所有这些 JSON 都作为数据传递到第二个 cURL 中。如何从 stdin 中选择最后一行,即应在第二个 cURL 中传递的实际数据?

armnotstrong建议的一个可能的方法帮助我把它做好了。虽然从技术上讲,这不是问题的答案,但这是一个很好的解决方法。

我正在错误流中打印我的输出,这些输出不需要发送到第二个 cURL。以下是工作网址:

curl -XPOST -H "Content-type: application/json" -d '{"a":1, "b" : 2}' 'https://a.com/a' | 
python -c "import json,sys;o=json.load(sys.stdin);o1=dict(id=o['foo'],hash=o['bar']);sys.stderr.write(json.dumps(o));print(json.dumps(o1));" |
curl -XPOST -H "Content-type: application/json" https://a.com/b -d @-

最新更新