'How to make a curl request with environmental variables
I am currently trying to execute the following command:
STAMP="4 day ago"
YESTERDAY=`date +%Y.%m.%d -d "$STAMP"`
#echo "test"
#echo "TEST" '"$YESTERDAY"'
DAYS="50 days ago"
DAYSTAMP=`date +%Y.%m.%d -d "$DAYS"`
curl -XPUT -i "http://localhost/_snapshot/repo/snapshot-2016.11.30?pretty" -d"
{
"indices": "filebeat-2016.11.30",
"ignore_unavailable": true,
"include_global_state": false
}"
# {"Message":"Your request: '/_snapshot/repo/snapshot-filebeat-2016.11.30' is not allowed due to invalid input parameters."}
curl -XPUT -i http://localhost/_snapshot/repo/snapshot-2016.11.30?pretty -d
{
indices: filebeat-2016.11.30,
ignore_unavailable: true,
include_global_state: false
}
The above only happens when I use double quotes. I use double quotes to access the environmental variables. If I replace quotes ("") with apostrophes (''), this command works but environmental variables do not.
Any help on how to plug the environmental variables into the curl request would be great
Solution 1:[1]
Single quotes inhibit variable substitution, so use double quotes. The inner double quotes must then be escaped.
... -d "{\"username\":\"$USERNAME\",\"password\":\"$PASSWORD\"}"
Solution 2:[2]
You can use bash heredoc for formatting message:
DAYSTAMP=2016.11.30
data=$(cat <<JSON_END
{
"indices": "filebeat-$DAYSTAMP",
"ignore_unavailable": true,
"include_global_state": false
}
JSON_END
)
curl -XPUT -i "http://localhost/_snapshot/repo/snapshot-2016.11.30?pretty" -d"$data"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user18780417 |
Solution 2 | Laurel |