78

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 https://www.mysite.com

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.

Bernhard Heijstek
  • 2,041
  • 1
  • 16
  • 20

7 Answers7

88

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 https://www.example.com

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" https://login.example.com/connect/token
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Bernhard Heijstek
  • 2,041
  • 1
  • 16
  • 20
  • 1
    I have 7.52.1 and I can't get this to work... I can do --data-urlencode "something=it's a test" but not with double quotes or @ symbols... – PJ Brunet Jan 20 '18 at 04:09
  • 1
    Remember to use `--data-urlencode` only for the data part that needs it. For example, the following will not work when you combine the data into one entity: `curl --data-urlencode "name=john&passwd=@31&3*J" https://www.example.com` – Mr-IDE Apr 27 '18 at 10:08
  • 2
    Exclamation points seem to cause problems with this in regards to history expansion in bash. You can fix it with single quotes around each piece of data (e.g. `--data-urlencode 'password=abc!123'` or by escaping the exclamation `--data-urlencode password=abc\!123`. More info: https://unix.stackexchange.com/questions/33339/cant-use-exclamation-mark-in-bash – 2xj Apr 01 '19 at 18:28
45

How about using the entity codes...

@ = %40

& = %26

So, you would have:

curl -d 'name=john&passwd=%4031%263*J' https://www.mysite.com

Biagio Arobba
  • 1,075
  • 11
  • 27
16

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

curl "http://www.example.com?name=john&passwd=@31&3*J"
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Thusitha Sumanadasa
  • 1,669
  • 2
  • 22
  • 30
13

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

passwd=\@31\&3*J
user3650655
  • 361
  • 3
  • 8
7

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 https://www.example.com
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Abdul Majeed
  • 2,671
  • 22
  • 26
5

Try this:

export CURLNAME="john:@31&3*J"
curl -d -u "${CURLNAME}" https://www.example.com
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
0

I did this

~]$ export A=g

~]$ export B=!

~]$ export C=nger


   curl http://<>USERNAME<>1:$A$B$C@<>URL<>/<>PATH<>/
Thusitha Sumanadasa
  • 1,669
  • 2
  • 22
  • 30
Lordlebu
  • 411
  • 1
  • 6
  • 15