curl post傳遞json數據


有時想在命令行使用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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM