[Note: I originally posted this problem/solution to Stackoverflow.com]
Problem:
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:
1 |
$ 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.
Solution:
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:
1 |
$ 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.