urlencoding for the impatient

Sometimes you’ll need to use curl to query a Rest API, but you realize you need to do a GET request for quite a complicated query-string (I am looking at you, Jira) that includes spaces and lots of special characters … which naturally will fail without proper url-encoding :-(
… like this very bad example below:

1
2
curl 'https://example.com/rest/v2/query=jql=project = "MY_PROJECT" AND date > "20220410"'
curl: (3) URL using bad/illegal format or missing URL

But fear not: curl to the rescue!

There are quite a few tools and ways to urlencode a string but I would like to keep it simple and just use my favourite tool: curl, without installing some other tool or script.

Studying the curl manual (try curl --manual), we find that a very handy option --data-urlencode CONTENT will url-encode CONTENT and add it as POST data to the POST request.
Too bad, because we need a GET request …

Adding option -X GET does not work because --data[-xxx] options override that and curl performs a POST request instead.

Looking further, we find another promising option: --get

1
2
3
4
5
 -G, --get
      When used, this option will make all  data  specified  with  -d,
      --data,  --data-binary or --data-urlencode to be used in an HTTP
      GET request instead of the POST request that otherwise would  be
      used. The data will be appended to the URL with a '?' separator.

Yes!! That’s exactly what we need! Lets try again:

1
2
3
4
curl https://example.com/rest/v2/query \
    --data-urlencode 'jql=project = "MY_PROJECT" AND date > "20220410"' \
    --get \
 

This results in a fine GET request with an url-encoded jql parameter

1
GET /rest/v2/query?jql=project+%3D+%22MY_PROJECT%22+AND+date+%3E+%2220220410%22
updatedupdated2022-05-102022-05-10