Linux命令

如何怎样用curl命令实现post请求

官方命令的帮助地址:https://curl.haxx.se/docs/manpage.html#-d

直接上命令实例了:
发送一个字段和值:
curl –data “param1=value1&param2=value2” https://moneyslow.com/ziyuan.cgi
多个字段值:
curl –data “param1=value1” –data “param2=value2” https://moneyslow.com/ziyuan.cgi
上传文件:
curl –form “fileupload=@my-file.txt” https://moneyslow.com/ziyuan.cgi
定义上传的文件名称:
curl –form “fileupload=@my-file.txt;filename=desired-filename.txt” –form param1=value1 –form param2=value2 https://moneyslow.com/ziyuan.cgi
发送空数据:
curl –data ” https://moneyslow.com/ziyuan.cgi
curl -X POST https://moneyslow.com/ziyuan.cgi
curl –request POST https://moneyslow.com/ziyuan.cgi
传输大文件数据:
curl –tr-encoding -X POST -v -# -o output -T filename.dat \
http://moneyslow.com/ziyuan.cgi
注意:-o output 这个参数必须有。

如果是 RESTful HTTP POST 包含 XML:
curl -X POST -d @filename.txt http://moneyslow.com/path/to/resource –header “Content-Type:text/xml”

或者是JSON:(读取filename.txt并post方式发送.)
curl -X POST -d @filename.txt http://moneyslow.com/path/to/resource –header “Content-Type:application/json”

如果是标准输入 stdin 需要用 -d @-
例子:
echo ‘{“text”: “Hello **world**!”}’ | curl -d @- https://moneyslow.com/markdown
输出结果:
<p>Hello <strong>world</strong>!</p>

对于特殊字符例如:
curl -d “name=Rafael%20Sagula&phone=3320780” http://www.moneyslow.com/guest.cgi
如果不执行,那么可以把&变更为%26:
curl -d “name=Rafael%20Sagula%26phone=3320780” http://www.moneyslow.com/guest.cgi

利用curl登录网站:
curl -d “username=admin&password=admin&submit=Login” –dump-header headers http://moneyslow.com/Login
curl -L -b headers http://moneyslow.com/