curl 命令模擬 HTTP GET/POST 請求

在 Linux 操做系統上對後端程序進行測試的時候,須要進行模擬鏈接或者書寫測試腳本css

curl html

訪問百度,經過GET方法請求json

命令格式: curl protocol://address:port/url後端

curl http://www.baidu.com
curl www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新聞</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地圖</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>視頻</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>貼吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登陸</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登陸</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多產品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關於百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必讀</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a>&nbsp;京ICP證030173號&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
View Code

獲取天氣數據,經過POST方法請求app

命令格式:curl -d "args"  "protocol://address:port/url"curl

curl -d "city=dongguan&appkey=8010132dcf54491a4eaa387f4db61774" "https://way.jd.com/he/freeweather"
curl -d "city=dongguan&appkey=8010132dcf54491a4eaa387f4db61774" "https://way.jd.com/he/freeweather"


{"code":"10000","charge":false,"msg":"查詢成功","result":{"HeWeather5":[{"now":{"hum":"67","vis":"7","pres":"1018","pcpn":"0","fl":"9","tmp":"16","cond":{"txt":"多雲","code":"101"},"wind":{"sc":"3-4","spd":"12","deg":"96","dir":"東風"}},"suggestion":{"uv":{"txt":"紫外線強度較弱,建議出門前塗擦SPF在12-15之間、PA+的防曬護膚品。","brf":""},"cw":{"txt":"較適宜洗車,將來一天無雨,風力較小,擦洗一新的汽車至少能保持一天。","brf":"較適宜"},"trav":{"txt":"天氣較好,但絲絕不會影響您出行的心情。溫度適宜又有微風相伴,適宜旅遊。","brf":"適宜"},"air":{"txt":"氣象條件對空氣污染物稀釋、擴散和清除無明顯影響,易感人羣應適當減小室外活動時間。","brf":""},"comf":{"txt":"白天不太熱也不太冷,風力不大,相信您在這樣的天氣條件下,應會感到比較清爽和溫馨。","brf":"溫馨"},"drsg":{"txt":"建議着薄外套、開衫牛仔衫褲等服裝。年老體弱者應適當添加衣物,宜着夾克衫、薄毛衣等。","brf":"較溫馨"},"sport":{"txt":"天氣較好,趕快投身大天然參與戶外運動,盡情感覺運動的快樂吧。","brf":"適宜"},"flu":{"txt":"各項氣象條件適宜,無明顯降溫過程,發生感冒機率較低。","brf":"少發"}},"aqi":{"city":{"no2":"27","o3":"71","pm25":"38","qlty":"","so2":"11","aqi":"54","pm10":"47","co":"1"}},"basic":{"city":"東莞","update":{"loc":"2017-12-05 17:52","utc":"2017-12-05 09:52"},"lon":"113.7462616","id":"CN101281601","cnty":"中國","lat":"23.04623795"},"daily_forecast":[{"date":"2017-12-05","pop":"0","hum":"46","uv":"6","vis":"16","astro":{"ss":"17:41","mr":"19:32","ms":"08:13","sr":"06:52"},"pres":"1022","pcpn":"0.0","tmp":{"min":"15","max":"20"},"cond":{"txt_n":"多雲","code_n":"101","code_d":"103","txt_d":"晴間多雲"},"wind":{"sc":"微風","spd":"3","deg":"0","dir":"無持續風向"}},{"date":"2017-12-06","pop":"0","hum":"41","uv":"6","vis":"20","astro":{"ss":"17:41","mr":"20:36","ms":"09:16","sr":"06:52"},"pres":"1019","pcpn":"0.0","tmp":{"min":"14","max":"22"},"cond":{"txt_n":"多雲","code_n":"101","code_d":"101","txt_d":"多雲"},"wind":{"sc":"微風","spd":"5","deg":"0","dir":"無持續風向"}},{"date":"2017-12-07","pop":"0","hum":"47","uv":"6","vis":"20","astro":{"ss":"17:41","mr":"21:40","ms":"10:14","sr":"06:53"},"pres":"1019","pcpn":"0.0","tmp":{"min":"14","max":"23"},"cond":{"txt_n":"多雲","code_n":"101","code_d":"100","txt_d":""},"wind":{"sc":"微風","spd":"9","deg":"0","dir":"無持續風向"}},{"date":"2017-12-08","pop":"0","hum":"36","uv":"6","vis":"20","astro":{"ss":"17:42","mr":"22:42","ms":"11:06","sr":"06:54"},"pres":"1022","pcpn":"0.0","tmp":{"min":"11","max":"20"},"cond":{"txt_n":"","code_n":"100","code_d":"100","txt_d":""},"wind":{"sc":"微風","spd":"9","deg":"351","dir":"北風"}},{"date":"2017-12-09","pop":"0","hum":"27","uv":"6","vis":"20","astro":{"ss":"17:42","mr":"23:42","ms":"11:53","sr":"06:54"},"pres":"1022","pcpn":"0.0","tmp":{"min":"10","max":"19"},"cond":{"txt_n":"","code_n":"100","code_d":"100","txt_d":""},"wind":{"sc":"微風","spd":"6","deg":"0","dir":"無持續風向"}},{"date":"2017-12-10","pop":"0","hum":"28","uv":"6","vis":"20","astro":{"ss":"17:42","mr":"09:47","ms":"12:36","sr":"06:55"},"pres":"1021","pcpn":"0.0","tmp":{"min":"10","max":"21"},"cond":{"txt_n":"","code_n":"100","code_d":"100","txt_d":""},"wind":{"sc":"微風","spd":"4","deg":"0","dir":"無持續風向"}},{"date":"2017-12-11","pop":"0","hum":"31","uv":"6","vis":"20","astro":{"ss":"17:42","mr":"00:38","ms":"13:16","sr":"06:56"},"pres":"1022","pcpn":"0.0","tmp":{"min":"11","max":"22"},"cond":{"txt_n":"多雲","code_n":"101","code_d":"100","txt_d":""},"wind":{"sc":"微風","spd":"6","deg":"0","dir":"無持續風向"}}],"hourly_forecast":[{"date":"2017-12-05 19:00","pop":"0","hum":"32","pres":"1020","tmp":"17","cond":{"txt":"晴間多雲","code":"103"},"wind":{"sc":"微風","spd":"16","deg":"43","dir":"東北風"}},{"date":"2017-12-05 22:00","pop":"0","hum":"36","pres":"1021","tmp":"17","cond":{"txt":"晴間多雲","code":"103"},"wind":{"sc":"微風","spd":"14","deg":"41","dir":"東北風"}},{"date":"2017-12-06 01:00","pop":"0","hum":"36","pres":"1020","tmp":"15","cond":{"txt":"","code":"100"},"wind":{"sc":"微風","spd":"12","deg":"41","dir":"東北風"}},{"date":"2017-12-06 04:00","pop":"0","hum":"39","pres":"1020","tmp":"15","cond":{"txt":"晴間多雲","code":"103"},"wind":{"sc":"微風","spd":"10","deg":"41","dir":"東北風"}},{"date":"2017-12-06 07:00","pop":"0","hum":"39","pres":"1021","tmp":"15","cond":{"txt":"晴間多雲","code":"103"},"wind":{"sc":"微風","spd":"9","deg":"46","dir":"東北風"}},{"date":"2017-12-06 10:00","pop":"0","hum":"35","pres":"1021","tmp":"16","cond":{"txt":"晴間多雲","code":"103"},"wind":{"sc":"微風","spd":"8","deg":"54","dir":"東北風"}},{"date":"2017-12-06 13:00","pop":"0","hum":"31","pres":"1019","tmp":"17","cond":{"txt":"晴間多雲","code":"103"},"wind":{"sc":"微風","spd":"5","deg":"69","dir":"東北風"}},{"date":"2017-12-06 16:00","pop":"0","hum":"32","pres":"1017","tmp":"20","cond":{"txt":"晴間多雲","code":"103"},"wind":{"sc":"微風","spd":"4","deg":"91","dir":"東風"}}],"status":"ok"}]}}
View Code

這種方法是參數直接在header裏面的ide

若是將輸出指定到文件能夠經過重定向進行操做測試

命令格式:curl -H "Content-Type:application/json" -X POST --data (json.data) URLui

curl -H "Content-Type:application/json" -X POST --data '{"message": "sunshine"}' http://localhost:8000/

這種方法是json數據直接在body裏面的url

 

更多 curl 用法或參數可以使用 man 命令查看,亦可訪問 https://curl.haxx.se/

相關文章
相關標籤/搜索