Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to urlencode data for curl command?

I am trying to write a bash script for testing that takes a parameter and sends it through curl to web site. I need to url encode the value to make sure that special characters are processed properly. What is the best way to do this?

Here is my basic script so far:

#!/bin/bash host=${1:?'bad host'} value=$2 shift shift curl -v -d "param=${value}" http://${host}/somepath $@ 
like image 632
Aaron Avatar asked Nov 17 '08 19:11

Aaron


People also ask

What is data URL encode in curl?

URL encoding. Percent-encoding, also known as URL encoding, is technically a mechanism for encoding data so that it can appear in URLs. This encoding is typically used when sending POSTs with the application/x-www-form-urlencoded content type, such as the ones curl sends with --data and --data-binary etc.

How do I encode in curl?

Use curl --data-urlencode ; from man curl : This posts data, similar to the other --data options with the exception that this performs URL-encoding. To be CGI-compliant, the <data> part should begin with a name followed by a separator and a content specification. See the man page for more info.

Do I need to URL encode POST data?

General Answer. The general answer to your question is that it depends. And you get to decide by specifying what your "Content-Type" is in the HTTP headers. A value of "application/x-www-form-urlencoded" means that your POST body will need to be URL encoded just like a GET parameter string.

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.


1 Answers

Use curl --data-urlencode; from man curl:

This posts data, similar to the other --data options with the exception that this performs URL-encoding. To be CGI-compliant, the <data> part should begin with a name followed by a separator and a content specification.

Example usage:

curl \     --data-urlencode "paramName=value" \     --data-urlencode "secondParam=value" \     http://example.com 

See the man page for more info.

This requires curl 7.18.0 or newer (released January 2008). Use curl -V to check which version you have.

You can as well encode the query string:

curl -G \     --data-urlencode "p1=value 1" \     --data-urlencode "p2=value 2" \     http://example.com     # http://example.com?p1=value%201&p2=value%202 
like image 97
Jacob Rask Avatar answered Sep 22 '22 10:09

Jacob Rask