I have multiple query parameters that I want to send in an HTTP PUT operation using curl. How do I encode the query parameters? Example:
$ curl -X PUT http://example.com/resource/1?param1=value%201¶m2=value2
If 'value 1' contains spaces or other characters that are interpreted by the shell, the command will not parse correctly.
cURL can also encode the query with the --data-urlencode parameter. When using the --data-urlencode parameter the default method is POST so the -G parameter is needed to set the request method to GET.
By default, Curl sends an HTTP POST request and posts the provided form data with the application/x-www-form-urlencoded content type.
cURL supports several different protocols, including HTTP and HTTPS, and runs on almost every platform.
The solution is to use the -G
switch in combination with the --data-urlencode
switch. Using the original example, the command would look like the following:
$ curl -X PUT -G 'http://example.com/resource/1' --data-urlencode 'param1=value 1' --data-urlencode param2=value2
The -G
switch causes the parameters encoded with the --data-urlencode
switches to be appended to the end of the http URL with a ?
separator.
In the example, the value of param1
, would be encoded as value%201
, where %20 is the encoded value for a space character.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With