Read HTTP Body Response and iterate over that result with Bash

Viewed 172

I'm pretty weak with bash so be patience with me.

I have to set a bash script that does this stuff:

  • Launch a curl request to an url like:
curl  https://example.com/my_first_request

This request return a simple response with a value per line like:

300
320
350
500

I need to read all this values and then do a curl request foreach line like this:

curl  https://example.com/my_second_request/300
curl  https://example.com/my_second_request/320
curl  https://example.com/my_second_request/350
curl  https://example.com/my_second_request/500

I don't care if those requests are async or sync, this script can take all the time it need.

I've tried multiple time with some answers on stackoverflow and some tutorials, but every time I fail. Can someone help me with this task?

Thanks.

2 Answers

You could use bash command substitution:

for code in $(curl https://example.com/my_first_request); do
    curl https://example.com/my_second_request/$code
done

You can try with xargs too, which is capable of running a specified number of commands in subprocesses. A quick one liner looks like:

curl "https://example.com/my_first_request" | xargs -n1 -P2 -I % curl "https://example.com/my_second_request/%"

The use of -n1 instructs xargs to process a single input argument at a time. In this case, the response codes (300, 320, 350, 500, ...) that you get from the first endpoint are each processed separately. And the -P2 tells xargs to maintain 2 sub-processes running all the time, each handles a single argument, until all of the input arguments are processed.

Related