Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include an '&' character in a bash curl statement

Tags:

bash

curl

Putting the entire URL inside double quotes should take care of your problem.


curl "http://www.example.com?m=method&args=1"

Are you using the & as a delimiter for a GET URL? Or is in a piece of data?

If it is in data you must encode it to an HTML character, if not, surround with quotes.

The encoding for & should become %26 in the URL.

curl "http://www.example.com?m=this%26that

Putting single quotes around the & symbol seems to work. That is, using a URL like http://www.example.com/page.asp?arg1=${i}'&'arg2=${j} with curl returns the requested webpage.


Instead of trying the escape "&" characters, you can specify http url parameters in POST requests with -d parameter as shown below:

curl -X POST http://www.example.com \
-d arg1=this \
-d arg2=that

If the request is a GET, then you should also add -G option, which tells curl to send the data with GET request.

curl -X GET -G http://www.example.com \
-d arg1=this \
-d arg2=that