This curl recipe shows you how to construct query strings for your GET requests. This is done via the -G
command line argument in combination with the -d
or --data-urlencode
arguments. The -G
argument will append the data specified in -d
and --data-urlencode
arguments at the end of the request URL, joining all data pieces with the &
character and separating them from the URL with the ?
character.
Construct Two Query Arguments
curl -G -d 'q=kitties' -d 'count=20' https://google.com/search
In this recipe, we let curl construct the query string and the final request URL for us. This recipe uses the -G
option and the -d
option twice that creates two query arguments. Curl joins them together like this q=kitties&count=20
and appends this string at the end of the https://google.com/search
request URL, and makes a GET request to https://google.com/search?q=kitties&count=20
. Be careful – if you forget the -G
argument, then curl will make a POST request instead!
URL-encode a Query Argument
curl -G --data-urlencode 'comment=this cookbook is awesome' https://catonmat.net
This recipe uses the --data-urlencode
argument. It works similar to the -d
argument but curl also URL-encodes the value. In this recipe, the comment
gets URL-encoded to this%20cookbook%20is%20awesome
and the GET request goes to https://catonmat.net?comment=this%20cookbook%20is%20awesome
.
Created by Browserling
These curl recipes were written down by me and my team at Browserling. We use recipes like this every day to get things done and improve our product. Browserling itself is an online cross-browser testing service powered by alien technology. Check it out!
Secret message: If you love my curl recipe, then I love you, too! Use coupon code CURLLING to get a discount at my company.