Home > Net >  How to use date variable in curl command
How to use date variable in curl command

Time:11-19

I've been attempting to simply create a curl command that uses the output of echo $(date '%Y-%d-%m') in the GET request that it makes. I've browsed the posts here and tried a number of solutions, but none seem to work for me.

Here is the example request:

curl -s -XGET 'https://www.website.com/api/shifts?start_date=2021-11-18

I have tried the following with no success:

curl -s -XGET 'https://www.website.com/api/shifts?start_date=`echo $(date  '%Y-%d-%m')`'
curl -s -XGET 'https://www.website.com/api/shifts?start_date='$(date  '%Y-%d-%m')'
DATE=$(date  '%Y-%d-%m') ; curl -s -XGET 'https://www.website.com/api/shifts?start_date="'"$DATE"'"'

Any input as to what I'm doing wrong would be greatly appreciated.

CodePudding user response:

The issue appears to be specifying the date format as %Y-%d-%m, whereas the string 2021-11-18 corresponds to %Y-%m-%d.

curl -qsSf "https://www.website.com/api/shifts?start_date=$(date  %Y-%m-%d)"

CodePudding user response:

A tangent: since bash v4.2, printf can format timestamps, so we don't even need to call out to date:

printf -v url 'https://www.website.com/api/shifts?start_date=%(%Y-%m-%d)T'
curl -qsSf "$url"

Providing no arguments means "current time"

  • Related