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:
|
|
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
|
|
Yes!! That’s exactly what we need! Lets try again:
|
|
This results in a fine GET request with an url-encoded jql parameter
|
|