cURL:在 POST 中从标准输入发送部分数据
cURL : Sending part data from stdin in 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']);print(json.dumps(o));print(json.dumps(o1));" | curl -XPOST -H "Content-type: application/json" https://a.com/b -d @-
在上面的 cURL 组合中,我通过 python 解析从第一个 cURL 接收到的数据,然后在 stdin 上打印接收到的输出和解析后的输出,然后将来自 stdin 的输入作为第二个 cURL 的数据。
问题:标准输入中打印了两个 JSON,所有这些都作为数据传递到第二个 cURL 中。我怎样才能 select stdin 的最后一行,这是应该在第二个 cURL 中传递的实际数据?
armnotstrong 建议的一种可能的方法帮助我做出正确的选择。尽管从技术上讲,这不是问题的答案,但它是一个很好的解决方法。
我正在错误流中打印不需要发送到第二个 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']);sys.stderr.write(json.dumps(o));print(json.dumps(o1));" |\
curl -XPOST -H "Content-type: application/json" https://a.com/b -d @-
我必须制作 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']);print(json.dumps(o));print(json.dumps(o1));" | curl -XPOST -H "Content-type: application/json" https://a.com/b -d @-
在上面的 cURL 组合中,我通过 python 解析从第一个 cURL 接收到的数据,然后在 stdin 上打印接收到的输出和解析后的输出,然后将来自 stdin 的输入作为第二个 cURL 的数据。
问题:标准输入中打印了两个 JSON,所有这些都作为数据传递到第二个 cURL 中。我怎样才能 select stdin 的最后一行,这是应该在第二个 cURL 中传递的实际数据?
armnotstrong 建议的一种可能的方法帮助我做出正确的选择。尽管从技术上讲,这不是问题的答案,但它是一个很好的解决方法。
我正在错误流中打印不需要发送到第二个 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']);sys.stderr.write(json.dumps(o));print(json.dumps(o1));" |\
curl -XPOST -H "Content-type: application/json" https://a.com/b -d @-