I am working on simple curl POST script and I have problem with string interpolation of a variable that is initialized with data from a script. Let me describe it:
#!/bin/bash
EMAIL=$(source ./random_email_generator.sh) #this generates some random string
echo "$EMAIL" #here email is printed well
curl --insecure --header "Content-Type: application/json" \
--request POST \
--data '{"email":'"$EMAIL"',"password":"validPassword"}' \ #problem lies here because email is always empty string
http:/ticketing.dev/api/users/signup
To sum up, there is a lot of quotes and double quotes here and it is abit mixed up, how can I resolve value of EMAIL in place of json body?
Thanks in advance
The variable is being interpolated properly. The problem is that you're not wrapping it in double quotes, so you're creating invalid JSON.
curl --insecure --header "Content-Type: application/json" \
--request POST \
--data '{"email":"'"$EMAIL"'","password":"validPassword"}' \
http:/ticketing.dev/api/users/signup
When you want to see what the command actually looks like, put echo before curl or run the script with bash -x. You would have seen
--data {"email":[email protected],"password":"validPassword"}
It would be better if you used the jq tool to manipulate JSON in bash scripts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With