Special Characters Like @ and & in cURL Post Data

How do I include special characters like @ and & in the cURL POST data? I'm trying to pass a name and password like:

curl -d name=john passwd=@31&3*J 

This would cause problems as @ is used for loading files and & for specifying more than one key/value. Is there some way I can escape these characters? \@ and \& don't seem to work.

7 Answers

cURL > 7.18.0 has an option --data-urlencode which solves this problem. Using this, I can simply send a POST request as

curl -d name=john --data-urlencode passwd=@31&3*J 

Summarizing the comments, in case of mixed "good" and "bad" data and exclamation marks inside we can use on Windows:

curl -d "grant_type=client_credentials&client_id=super-client&acr_values=tenant:TNT123" --data-urlencode "client_secret=XxYyZ21D8E&%fhB6kq^mXQDovSZ%Q*!ipINme"  
3

How about using the entity codes...

@ = %40

& = %26

So, you would have:

curl -d 'name=john&passwd=%4031%263*J'

3

Double quote (" ") the entire URL .It works.

curl ""
1

Just found another solutions worked for me. You can use '\' sign before your one special.

passwd=\@31\&3*J
0

Try this:

export CURLNAME="john:@31&3*J"
curl -d -u "${CURLNAME}" 
1

If password has the special characters in it, just round the password with the single quote it will work.

curl -u username:'this|!Pa&*12' --request POST 
1

I did this

~]$ export A=g

~]$ export B=!

~]$ export C=nger


   curl 
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest