简体   繁体   中英

Check if curl response is empty

I have a simple shell script where I am doing a cURL and based on the response if empty or not wants to log a flag. I tried null check but it still goes to else part.

Script: test.sh

    RESP=`curl  -m 600 -X POST -H "Accept: application/json" ......`  #curl response
    #if [ -z "$RESP" ] && echo "Empty"
    if [ ${#RESP[@]} -eq 0 ];
    then
     echo "APIName=Test1Api,  TicketRaised=N"
    else
     echo "RESPONSE is NOT NULL"
     echo -e "APIName=Test1Api, HTTP_STATUS=$HTTP_STATUS, totalTime=$TIME_TAKEN, Response=$RESP, TicketRaised=Y"
    fi
    
    

Output > (even if Response ie "RESP" if empty it goes into else part)

RESPONSE is NOT NULL
APIName=Test1Api, HTTP_STATUS=[], totalTime=545, Response=[], TicketRaised=Y

I deduce from your answer that what you call an empty answer is the empty JASON string [] .

So try:

RESP=`curl  -m 600 -X POST -H "Accept: application/json" ......`  #curl response
if [ $RESP = "[]" ]
then
  echo "APIName=Test1Api,  TicketRaised=N"
else
  echo "RESPONSE is NOT NULL"
  echo -e "APIName=Test1Api, HTTP_STATUS=$HTTP_STATUS, totalTime=$TIME_TAKEN, Response=$RESP, TicketRaised=Y"
fi

Be sure that there is no newline char after [] . Otherwise, you will have to adapt the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM