0

I've to make 2 cURLs. The data from 1 curl needs to be formatted & sent as an input in the 2nd cURL. I've attempted the following:

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 @-

In the above combination of cURLs, I'm parsing the data received from 1st cURL via python and then print both the received output & the parsed output on stdin, then take the input from stdin as the data for 2nd cURL.

Problem: There are two JSONs which are being printed in stdin and all of that is being passed as data in 2nd cURL. How can I select just the last line from stdin which is the actual data that should be passed in the 2nd cURL?

Pankaj Singhal
  • 15,283
  • 9
  • 47
  • 86

1 Answers1

2

A possible approach suggested by armnotstrong helped me in getting it right. Though this is, technically, not an answer to the question but it is a good workaround.

I'm printing my outputs, which are not required to send to the 2nd cURL, in error stream. Following is the working 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 @-
Pankaj Singhal
  • 15,283
  • 9
  • 47
  • 86
  • @sergio I appreciate your edit but I personally don't feel comfortable with it coz it hampers readability. By converting it into `code block`, it becomes way too horizontally long. If someone is trying to understand it, it helps if it's compact. Else, by the time some reach the rightmost part, (s)he loses the context of the left-most part &, it's way too much too-and-fro. – Pankaj Singhal Aug 30 '20 at 12:14
  • @sergio Have done a little modification to your edit to make it more readable. – Pankaj Singhal Aug 30 '20 at 12:20