【发布时间】:2017-06-24 11:19:00
【问题描述】:
我要制作 2 个 cURL。来自 1 curl 的数据需要格式化并作为第二个 cURL 中的输入发送。我尝试了以下方法:
curl -XPOST -H "Content-type: application/json" -d '{"a":1, "b" : 2}' 'https://a.com/a' | python -c "导入 json,sys;o=json.load(sys.stdin);o1=dict(id=o['foo'],hash=o['bar']);print(json.dumps(o ));打印(json.dumps(o1));" | curl -XPOST -H "Content-type: application/json" https://a.com/b -d @-
在上述 cURL 组合中,我通过 python 解析从第一个 cURL 接收的数据,然后在标准输入上打印接收到的输出和解析的输出,然后将标准输入的输入作为第二个 cURL 的数据。
问题:有两个 JSON 在标准输入中打印,所有这些都作为数据在第二个 cURL 中传递。如何从 stdin 中选择最后一行,这是应该在第二个 cURL 中传递的实际数据?
【问题讨论】:
-
那你为什么在你的python代码中
print(json.dumps(o))?可以省略吗? -
我也想知道第一个 cURL 的输出。
-
在这种情况下,我认为将第一个 cURL 结果打印到stderr 是个好主意。