代码改变世界

curl post传递json数据

2014-03-27 12:50  youxin  阅读(12047)  评论(0编辑  收藏  举报

有时想在命令行使用post

http提交一个表单,比较常用的是POST模式和GET模式

GET模式什么option都不用,只需要把变量写在url里面就可以了

比如:curl http://www.waynerQiu.com/login.cgi?user=nickwolfe&password=12345

而POST模式的option则是 -d (--data

-d/--data <data>
(HTTP) Sends the specified data in a POST request to the HTTP
server, in the same way that a browser does when a user has filled
in an HTML form and presses the submit button. This will cause curl
to pass the data to the server using the content-type >application/x-
www-form-urlencoded.

比如,curl -d "user=nickwolfe&password=12345" http://www.waynerQiu.com/login.cgi

就相当于向这个站点发出一次登陆申请;

到底该用GET模式还是POST模式,要看对面服务器的程序设定。

curl --data "data=xxx" example.com/form.cgi

如果你的数据没有经过表单编码,还可以让curl为你编码,参数是--data-urlencode。

  curl --data-urlencode "date=April 1" example.com/form.cgi

一点需要注意的是,POST模式的文件上传,比如

<form method="POST" enctype="multipar/form-data" 

action="http://... /~zzh/up_file.cgi">

<input type=file name=upload>

<input type=submit name=nick value="go">

</form>

这样一个HTTP表单,我们要用curl进行模拟,就该是这样的语法:

curl -F upload=@localfile -F nick=go http://.../~zzh/up_file.cgi

11)https的时候使用本地证书,可以使用option:-E

curl -E localcert.pem https://remote_server

更多:

http://stackoverflow.com/questions/7172784/how-to-post-json-data-with-curl-from-terminal-commandline-to-test-spring-rest

http://www.dewen.org/q/3803/%E5%9C%A8linux%E5%91%BD%E4%BB%A4%E8%A1%8C%E4%B8%8Bcurl+%E6%8F%90%E4%BA%A4json%E6%95%B0%E6%8D%AE%E7%9A%84%E9%97%AE%E9%A2%98