一個簡單的GET請求
使用curl命令可以輕松發起一個HTTP請求:
# 使用GET凡是請求網址
curl http://www.baidu.com
可以使用-X選項指定請求方式
攜帶參數的POST請求
下面演示一個帶頭部和參數的POST請求
curl -X POST \
'http://uusama.com/?r=SnapchatApi%2FdoCurlQuery' \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-F name=uusama \
-F like=fruit
該請求方式相當於在頁面提交一個表單,其中:
-X POST 指定請求凡是為POST請求
-H 指定請求頭部
-F 指定請求參數
curl命令測試請求耗時
在curl命令中,有以下幾個變量反應請求時間:
time_namelookup:DNS解析域名時間,把域名—>ipd的時間
time_connect:TCP連接的時間,三次握手的時間
time_appconnect:SSL|SSH等上層連接建立的時間
time_pretransfer:從請求開始到到響應開始傳輸的時間
time_redirect:從開始到最后一個請求事務的時間
time_starttransfer:從請求開始到第一個字節將要傳輸的時間
time_total:總時間
示例:
curl -o /dev/null -s -w time_namelookup:"\t"%{time_namelookup}"\n"time_connect:"\t\t"%{time_connect}"\n"time_appconnect:"\t"%{time_appconnect}"\n"time_pretransfer:"\t"%{time_pretransfer}"\n"time_starttransfer:"\t"%{time_starttransfer}"\n"time_total:"\t\t"%{time_total}"\n"time_redirect:"\t\t"%{time_redirect}"\n" http://www.baidu.com
time_namelookup: 5.520
time_connect: 5.543
time_appconnect: 0.000
time_pretransfer: 5.543
time_starttransfer: 5.566
time_total: 5.566
time_redirect: 0.000
其中各選項的含義如下:
-w:將請求結果輸入到文件而不是標准輸出
-o:請求完成后使用自定義格式輸出
-s:靜默模式(不要輸出任何東西)