Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curl command line encoding of query parameters for an HTTP PUT

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&param2=value2

If 'value 1' contains spaces or other characters that are interpreted by the shell, the command will not parse correctly.

like image 464
2Aguy Avatar asked Oct 06 '15 21:10

2Aguy


People also ask

Does curl encode query parameters?

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.

Does curl encode post data?

By default, Curl sends an HTTP POST request and posts the provided form data with the application/x-www-form-urlencoded content type.

Does curl use HTTP?

cURL supports several different protocols, including HTTP and HTTPS, and runs on almost every platform.


1 Answers

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.

like image 58
2Aguy Avatar answered Sep 21 '22 21:09

2Aguy