0x00b0
0x00b0

Reputation: 363

How to save the response body from cURL in a file when executing the command in a loop in a bash script?

I've created a cURL bash script in which I want to save the response body into a file called output.log, but when I open the file output.log it looks like this:

screenshot of the output.log file showing a line for the Header Code, a line for the execution date and HTTP/1.1 for the Response Body

Here is my bash script:

#!/bin/bash
SECRET_KEY='helloWorld'
FILE_NAME='sma.txt'

function save_log()
{

    printf '%s\n' \
    "Header Code    : $1" \
    "Executed at    : $(date)" \
    "Response Body  : $2" \
    '==========================================================\n'  > output.log
}

while IFS= read -r line; 
    do 
        HTTP_RESPONSE=$(curl -I -L -s -w "HTTPSTATUS:%{http_code}\\n" -H "X-Gitlab-Event: Push Hook" -H 'X-Gitlab-Token: '$SECRET_KEY --insecure $line 2>&1)
        HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')

        save_log $HTTP_STATUS $HTTP_RESPONSE
done < $FILE_NAME

Can anyone help me get my desired output in my output.log?

Upvotes: 2

Views: 1906

Answers (1)

Mark Westenberg
Mark Westenberg

Reputation: 21

From the Curl documentation: -I, --head Show document info only Removing the -I or replace it with -i should solve your problem

Upvotes: 2

Related Questions