CURL POST 数据中的@和 & 等特殊字符

如何在 cURL POST 数据中包含像@和 & 这样的特殊字符?我试图传递一个名称和密码,比如:

curl -d name=john passwd=@31&3*J https://www.mysite.com

这将导致问题,因为 @用于加载文件,而 &用于指定多个键/值。有什么办法能让我摆脱这些角色吗?\@\&似乎不起作用。

194005 次浏览

How about using the entity codes...

@ = %40

& = %26

So, you would have:

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

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

I did this

~]$ export A=g


~]$ export B=!


~]$ export C=nger




curl http://<>USERNAME<>1:$A$B$C@<>URL<>/<>PATH<>/

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

curl "http://www.mysite.com?name=john&passwd=@31&3*J"

Try this:

export CURLNAME="john:@31&3*J"
curl -d -u "${CURLNAME}" https://www.example.com

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

passwd=\@31\&3*J

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