0

In the following code/file, I am trying to get current date in the final email.

But what I am getting is the variable $DATE itself in the email. Same for $from_email and $to_email.

My question: is there a different way/method than using ' in --data?

Here's my code:

NL=$'\n'
DATE=$(date)
DATE=${DATE%$NL}
echo $DATE
#
curl --request POST \
  --url https://api.sendgrid.com/v3/mail/send \
  --header "Authorization: Bearer $SENDGRID_CURL_API_KEY" \
  --header 'Content-Type: application/json' \
  --data '\
{
        "personalizations": [{"to": [{"email": "$to_email"}]}],\
        "from": {"email": "$from_email"},\
        "subject": "SendGrid API $DATE",\
        "content": [{"type": "text/plain", "value": "Sent on $DATE using Shell/Curl"}]\
}\
'
  • Read up on the difference between single and double quotes. – Shawn Aug 27 '20 at 19:23
  • (In cases like this, I'm a fan of using `jq` to build the json and passing variables to it) – Shawn Aug 27 '20 at 19:24
  • Example: https://stackoverflow.com/a/62729928/9952196 – Shawn Aug 27 '20 at 19:28
  • And https://stackoverflow.com/questions/13799789/expansion-of-variables-inside-single-quotes-in-a-command-in-bash for a more generic approach – Shawn Aug 27 '20 at 19:29
  • @Shawn I tried to use the generic approach. `--data '\ { \ "personalizations": [ {"to": [ {"email": ' "$to_email" '} ]} ], \ "from": {"email": ' "$from_email" '}, \ "subject": "SendGrid API ' "$DATE" ', \ "content": [{"type": "text/plain", "value": "Sent on ' "$DATE" ' using Shell/Curl"}] \ }'` But, with this, ,I am getting this error: `{"errors":[{"message":"Bad Request","field":null,"help":null}]}` – Bislinks Dotcom Aug 27 '20 at 20:45
  • Are you adding the needed double quotes around strings in the json? (One reason why I prefer the jq approach; much less error prone) – Shawn Aug 27 '20 at 21:08

0 Answers0