简体   繁体   中英

Pass $ variable to curl command in a bash script

I'm attempting to create a looping script that pulls phone numbers from a CSV file and sets the $from and $to variables with each loop.

However, I'm receiving an error reading "The supplied JSON is invalid". $body is passed properly due to the variable being set within the script itself, and no error is received when I hard code the $from and $to variables within the script.

However, as soon as I try to set the variables with data from the CSV, I receive a JSON error upon execution.

I've played with every possible variation of quotation marks within --data, but I've had no luck.

I've attached a screenshot of the sample data I'm attempting to pass to the script.

Any ideas? [![enter image description here][1]][1]

OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }

while read class from to
do
   if [[ $class = "send" ]];
   then
       echo "class: $class"
       echo "from: $from"
       echo "to: $to"

       body="Test"


echo $body
       
        curl -X POST \
        --header "Content-Type: application/json" \
        --header "Accept: application/json" \
        --header "Authorization: Bearer KEY018435A- SAMPLE" \
        --data '{
            "from": "'"$from"'",
            "to": "'"$to"'",
            "text": "'"$body"'",
            "media_urls" : [
            "https://files-service-prod.s3.amazonaws.com/direct-uploads%2F6e1a1600"
            ]
        }' \
        https://api.sortor.io/v2/messages


    fi    

sleep 2
done < $INPUT
IFS=$OLDIFS ```


  [1]: https://i.stack.imgur.com/9lkp4.png

Like this, using shell heredoc :

curl -X POST \
    --header "Content-Type: application/json" \
    --header "Accept: application/json" \
    --header "Authorization: Bearer KEY018435A- SAMPLE" \
    --data "@/dev/stdin" https://api.sortor.io/v2/messages <<EOF
    {
        "from": "$from",
        "to": "$to",
        "text": "$body",
        "media_urls" : [
            "https://files-service-prod.s3.amazonaws.com/direct-uploads%2F6e1a1600"
        ]
    }
EOF

Check

man bash | less +/here-doc

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