How do I put "%20" as a string in curl data?

872 views Asked by At

I am trying to put "%20" in the bash date format without it being interpreted. I am using the date string as a parameter in a curl request.
This was my approach in making a call to an api whose parameter includes a date string formatted with a space (YYYY-MM-DD HH:MM:SS).
The api's doc gives an example as "date=2014-04-30%2002:18:36".
But the company's web version of the api shows "date=2014-04-30+02%3A18%3A36".

I've tried date=$(date +%Y-%m-%d%%20%H:%M:%S) in my shell script, which does produce 2016-12-19%2016:03:26, but is still not working correctly.
I've also tried curl --data-urlencode without any success.

Are my approaches just totally wrong?

Update for additional information: I've tried date=$(date +%Y-%m-%d+%H%%3A%M%%3A%S), which gives me 2016-12-19+16%3A17%3A41. But the returned data is still inaccurate.
The version on the website accepts "2016-12-12 00:00:00" and I am able to get accurate data.

1

There are 1 answers

0
Daniel Stenberg On BEST ANSWER

One version to get it into a shell variable and then to curl:

#!/bin/bash
output=$(date "+%Y-%m-%d %H:%M:%S" | sed 's/ /%20/g')
curl -d date=$output http://example.com

One way to let curl do the URL encoding itself:

curl --data-urlencode date=$(date "+%Y-%m-%d %H:%M:%S") http://example.com