Saturday, October 29, 2022

[SOLVED] How to get Http Status Code and Vourbose logs by Curl function

Issue

We try to check the API connection and response by several curl commands as follows, we have two issue though.

-- function
function exec_cmd() {
cmd=$@
echo "curl command:" ${cmd}
response=$(${cmd} --max-time ${timeout_sec} -o ${LogFile} -w '%{http_code}\n' -S)
echo statud_code:${response}
}

-- curl command#1 ...
exec_cmd curl -v -x put <LBFrontIP/ApiEndpoint1> -H 'Content-Type:application/json; charset=utf-8' -H 'Host:<HostName>' -d '{"SystemId":"XXXX", "AppNo":"99999999999"}'

-- curl command#2
exec_cmd curl -v -x put <LBFrontIP/ApiEndpoint2> -H 'Content-Type:application/json; charset=utf-8' -H 'Host:<HostName>' -d '{"sbSystemId":"XXXX", "email":"[email protected]", 
"PhoneNo":"99999999999"}'

-- curl command#3
exec_cmd curl -v -x put <LBFrontIP/ApiEndpoint3> -H 'Content-Type:application/json; charset=utf-8' -H 'Host:<HostName>' -d '
{
    "ID1": "XXXXX",
    "ID2": "1q2w3e4r5t",
    "applyNo": "00db182faef74e779ca958681127bcec", ...
    "docLiveScore": "0.1391"
}'

Issue #1 : cmd=$@ can get the curl command but if that curl command contains @ or return code, it can't work as expected. curl command#1 is working fine, but curl command#2 and #3 is not, since bash misunderstands @ and return code in data option as terminate of the command line. Curl command can work by itself though. Also tried cmd=$* or cmd="$@" but not working so far.

Issue #2 : We'd like to get status_code for our quick check, but on the same time, we'd like to get the detail response in the log. Is there any better way to get both of status_code and verbouse log by appropriate function? Above commands over write the verbouse logs...

Any advice would be highly appreciated. Thank you in advance.


Solution

Given the complexity of the commands involved, I am tempted to say that you should avoid command parsing and read the command lines from a job configuration file.

#!/bin/bash

START=`pwd`
echo ${START}
BASE=`basename "$0" ".sh" `
ThisDATE=`date '+%Y%m%d_%H%M%S' `

LogPrefix="${START}/${BASE}.${ThisDATE}"        ; rm -f "${LogPrefix}"*
BatchFile="${START}/${BASE}.${ThisDATE}.config"     ; rm -f "${BatchFile}"

cat >$BatchFile <<-!EnDoFbAtCh
curl -v -x put <LBFrontIP/ApiEndpoint1> -H 'Content-Type:application/json; charset=utf-8' -H 'Host:<HostName>' -d '{"SystemId":"XXXX", "AppNo":"99999999999"}'
curl -v -x put <LBFrontIP/ApiEndpoint2> -H 'Content-Type:application/json; charset=utf-8' -H 'Host:<HostName>' -d '{"sbSystemId":"XXXX", "email":"[email protected]", "PhoneNo":"99999999999"}'
curl -v -x put <LBFrontIP/ApiEndpoint3> -H 'Content-Type:application/json; charset=utf-8' -H 'Host:<HostName>' -d '{ "ID1": "XXXXX", "ID2": "1q2w3e4r5t", "applyNo": "00db182faef74e779ca958681127bcec", ...  "docLiveScore": "0.1391" }'
!EnDoFbAtCh

function exec_cmd() {
    echo "curl command: ${cmd}"
    ${cmd} --max-time ${timeout_sec} -o ${LogFile} -w '%{http_code}\n' -S
    RC=$?
    echo "statud_code:${RC}"
}

index=1
while read cmd
do
    if [ -z "${cmd}" ] ; then  break ; fi

    LogFile="${LogPrefix}.${index}.log"

    exec_cmd

    index=`expr ${index} + 1 `
done < "${BatchFile}"


Answered By - Eric Marceau
Answer Checked By - Katrina (WPSolving Volunteer)