1

I am using this bash command to write the json http response body into a file:

curl -s "http://some.domain.com/getSomeJson" | jq . | tee someJson.json;

But what I really want is something like this:

curl -s "http://some.domain.com/getSomeJson" | jq . | tee someJson-THE_VALUE_OF_A_SPECIFIC_RESPONSE_HEADER_NAMED_X-FOO-BAR.json

So, in natural language:
I want to write the response of the cURL request into a file, preserving its pretty formatted json content, while at the same time use one of the response headers as part of the filename of said file.
I don't really care at which point the jq part happens in the command as long as the file content in the end is pretty formatted json.

kiltek
  • 141

1 Answers1

2

The json file will be created when you run the pipe. In your case if the file name cannot be finalized before starting the pipe, you have to break the pipe.

You can use -D and the and -o flags to dump the response header and the response body to two seperate temporary files. Later you can extract data from these temporary files to compose the file name and then write the response body.

header_file="/path/to/a/file";
body_file="/path/to/anotherfile";
curl -s \
     -D $header_file \
     -o $body_file \
     "http://some.domain.com/getSomeJson";
some_header=`grep THE_VALUE_OF_A_SPECIFIC_RESPONSE_HEADER_NAMED_X-FOO-BAR $header_file | cut -d ' ' -f2 | tr -d "\n" | tr -d "\r"`
jq . $body_file | tee "someJson-$some_header.json"
kiltek
  • 141
Taylor G.
  • 136