0

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.sstatic.net/9lkp4.png
2
  • 1
    You should rarely change IFS globally. You can change it just for the read command with while IFS=, read class from to; do .... Commented Dec 7, 2022 at 22:44
  • 1
    Don't try to construct JSON by hand; use a tool like jq to construct it for you. Commented Dec 7, 2022 at 22:44

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

I appreciate your help! No luck here though, unfortunately. When executing, I receive the following syntax error: "line 39: syntax error: unexpected end of file" Here is the code, and I attached a new screenshot to the original question if that helps with context.
The last EOF have to be at the beginning of a line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.