国产欧美日韩第一页|日本一二三不卡视频|在线精品小视频,亚洲第一免费播放区,metcn人体亚洲一区,亚洲精品午夜视频

幫助中心 >  行業(yè)資訊 >  云計(jì)算 >  curl命令使用場景總結(jié)

curl命令使用場景總結(jié)

2025-03-10 14:37:08 1939

在網(wǎng)絡(luò)請求與數(shù)據(jù)交互領(lǐng)域,curl 憑借其輕量、靈活的特性成為開發(fā)者與運(yùn)維人員的必備工具。無論是日常接口調(diào)試、文件傳輸,還是復(fù)雜的性能分析,curl 都能通過豐富的參數(shù)組合實(shí)現(xiàn)高效操作。本文以 2025 年 3 月 10 日為時(shí)間背景,系統(tǒng)梳理 curl 的 10 類高頻使用場景,結(jié)合實(shí)例與輸出示例,幫助大家快速掌握其核心用法。

1. GET請求

格式:curl -i [request-url]

  • -i 結(jié)果中輸出響應(yīng)頭信息

curl -i "http://www.tjdsmy.cn/uacserver/user/personalsettings?userId=20722351"

輸出結(jié)果示例:

HTTP/1.1 200
Server: nginx/1.13.7
Date: Tue, 10 Oct 2023 02:45:37 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
processId: dc5a8a60-d37b-4e87-b111-dd6eab484b17
upstream_http_reqid: dc5a8a60-d37b-4e87-b111-dd6eab484b17
processTime: 4
upstream_http_time: 4

…… 數(shù)據(jù)內(nèi)容

2. POST請求

  • -X 指定method

  • -d 指定發(fā)送Http Post data,content-type不限

curl -i -X POST -d 'data={"timenewsID":"1447","shareScope":{"type":"0","scope_id":[0]}}' 'http://www.tjdsmy.cn/microblog/timenews/modifysharescope?user_id=62051317&session_id=2dc60ccf24a6088a1e6a638205ed5f66f11dac97'

3. 發(fā)送Form表單

  • -F 指定form-data形式來發(fā)送請求,參數(shù)為&分隔開的多個參數(shù)

curl -i -X POST -F "user_id=1453280&session_id=61f730d921eed96d88f34cb18d0e592d6f21202b" 'http://www.tjdsmy.cn/uccserver/uccapi/user/check'

4. 請求體urlencode編碼

  • –data-urlencode,通過該選項(xiàng)提供的參數(shù)會自動轉(zhuǎn)義特殊字符

curl --data-urlencode "value& 1" http://www.tjdsmy.cn

5. 發(fā)送JSON

  • -H 指定Header,需要用雙引號,可以多次使用指定多個Header

curl -i -X POST -H "Content-Type: application/json" -d "{"newid":"1447"}'" 'http://www.tjdsmy.cn/microblog/timenews/timenewpraise/1447'

6. 下載文件

使用-O或-o選項(xiàng)來指明將輸出內(nèi)容以文件形式下載。

  • -O:使用URL中默認(rèn)的文件名保存內(nèi)容到本地,此選項(xiàng)會自動輸出進(jìn)度信息

curl -O http://www.tjdsmy.cn/software/gettext/manual/gettext.html
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                Dload  Upload   Total   Spent    Left  Speed
100 1556k  100 1556k    0     0   121k      0  0:00:12  0:00:12 --:--:--  135k

  • -o:將內(nèi)容保存到命令行指定的文件中,此選項(xiàng)會自動輸出進(jìn)度信息


curl -o mygettext.html http://www.tjdsmy.cn/software/gettext/manual/gettext.html
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                Dload  Upload   Total   Spent    Left  Speed
100 1556k  100 1556k    0     0   164k      0  0:00:09  0:00:09 --:--:--  182k

7. 上傳文件

格式:curl -F “file=@/path/to/file” URL

  • -F 選項(xiàng)也可以用于上傳文件

  • file=@/path/to/file表示要上傳的文件路徑

  • 當(dāng)上傳文件的同時(shí)也需要提交form表單,則可以指定兩次-F選項(xiàng)

curl -F "userId=88407056" -F "file=@mygettext.html" "http://www.tjdsmy.cn/eamsgateway/eams-support/setting/uploadFile"

8. 輸出調(diào)試信息

-v 輸出詳細(xì)的調(diào)試信息,包括請求頭、響應(yīng)頭以及http請求的整個過程

curl -v -F "file=@mygettext.html" "http://www.tjdsmy.cn/eamsgateway/eams-support/setting/uploadFile"

調(diào)試信息示例:

*   Trying 10.255.0.71...
* TCP_NODELAY set
* Connected to testweb.quanshi.com (10.255.0.71) port 80 (#0)
> POST /eamsgateway/eams-support/setting/uploadFile HTTP/1.1
> Host: testweb.quanshi.com
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Length: 1594364
> Content-Type: multipart/form-data; boundary=------------------------3fa4d90f3c8f29f3
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 200 OK
< Server: nginx
< Date: Tue, 10 Oct 2023 04:57:30 GMT
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Encoding
< Vary: Accept-Encoding
< Access-Control-Allow-Methods: POST,GET,OPTIONS,DELETE
< Access-Control-Max-Age: 3600
< Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Access-Control-Allow-Origin,Accept,cache-control,if-modified-since
< Set-Cookie: JSESSIONID=F6921C9D1FADEA898E2BD5B9C935E4A8; Path=/; HttpOnly
<
* Connection #0 to host testweb.quanshi.com left intact
…… 響應(yīng)body
* Closing connection 0

9. 走代理發(fā)請求

  • -x 可以為CURL添加代理功能,請求會通過代理服務(wù)器發(fā)送出去

curl -x proxy.com:3128 http://www.tjdsmy.cn

10. 分析請求耗時(shí)

  • -w 自定義輸出內(nèi)容和格式

  • -o /dev/null 用于屏蔽響應(yīng)內(nèi)容,只關(guān)心統(tǒng)計(jì)信息

  • -s 用于屏蔽進(jìn)度條和錯誤信息的顯示

curl -o /dev/null -s -w "\\nhttp_code: %{http_code}\\ntime_namelookup: %{time_namelookup}s\\ntime_connect: %{time_connect}s\\ntime_starttransfer: %{time_starttransfer}s\\ntime_total: %{time_total}s\\n" "http://www.tjdsmy.cn/umsapi/rs/users/64042216/organizations"

自定義輸出格式時(shí)可以使用的內(nèi)置變量有:

  • %{time_total}: 請求花費(fèi)的總時(shí)間(秒)

  • %{time_namelookup}: DNS解析時(shí)間(秒),就是將域名轉(zhuǎn)換成IP地址的耗時(shí)

  • %{time_connect}: 建立連接時(shí)間(秒),可以理解成TCP協(xié)議三次握手的時(shí)間

  • %{time_appconnect}: SSL/TLS握手時(shí)間(秒)

  • %{time_pretransfer}: 從請求開始到請求數(shù)據(jù)開始傳輸?shù)臅r(shí)間(秒)

  • %{time_redirect}: 重定向時(shí)間(秒)

  • %{time_starttransfer}: 從請求開始到第一個字節(jié)接收的時(shí)間(秒)

  • %{speed_download}: 下載速度(字節(jié)/秒)

  • %{speed_upload}: 上傳速度(字節(jié)/秒)

  • %{size_download}: 下載文件的大?。ㄗ止?jié))

  • %{size_upload}: 上傳文件的大小(字節(jié))

  • %{http_code}: HTTP狀態(tài)碼

  • %{url_effective}: 請求的最終URL

  • %{redirect_url}: 重定向URL(如果有的話)

復(fù)雜的輸出格式可以用文件來定義,方式如下:

  • 先編輯一個文件來描述格式信息:vi curl-format.txt

   time_namelookup:  %{time_namelookup}s\\n
      time_connect:  %{time_connect}s\\n
   time_appconnect:  %{time_appconnect}s\\n
     time_redirect:  %{time_redirect}s\\n
  time_pretransfer:  %{time_pretransfer}s\\n
time_starttransfer:  %{time_starttransfer}s\\n
                   ----------\\n
        time_total:  %{time_total}s\\n

  • 發(fā)送請求時(shí)指定要采用的格式文件

curl -s -o /dev/null -w "@curl-format.txt" "http://www.tjdsmy.cn/umsapi/rs/users/64042216/organizations"

  • 輸出示例:

   time_namelookup:  0.002610s
      time_connect:  0.022759s
   time_appconnect:  0.000000s
     time_redirect:  0.000000s
  time_pretransfer:  0.022797s
time_starttransfer:  0.050744s
                   ----------
        time_total:  0.050963s

耗時(shí)信息的簡單理解:

  • TCP 連接時(shí)間 = pretransfter - namelookup

  • 服務(wù)器處理時(shí)間 = starttransfter - pretransfer

  • 內(nèi)容傳輸時(shí)間 = total - starttransfer

掌握 curl 的多樣化指令,不僅能提升開發(fā)效率,更能深入理解 HTTP 協(xié)議細(xì)節(jié)。從基礎(chǔ)的請求構(gòu)造到高級的耗時(shí)分析,本文通過典型案例展現(xiàn)了 curl 的強(qiáng)大功能。在實(shí)際應(yīng)用中,建議結(jié)合具體場景靈活組合參數(shù),同時(shí)關(guān)注輸出中的響應(yīng)頭、狀態(tài)碼及性能指標(biāo),以實(shí)現(xiàn)精準(zhǔn)的問題定位與優(yōu)化。期待這些實(shí)用技巧能成為工具箱中的利刃。


藍(lán)隊(duì)云官網(wǎng)上擁有完善的技術(shù)支持庫可供參考,大家可自行查閱,更多技術(shù)問題,也可以直接咨詢。同時(shí),藍(lán)隊(duì)云整理了運(yùn)維必備的工具包免費(fèi)分享給大家使用,需要的朋友可以直接咨詢。更多技術(shù)知識,藍(lán)隊(duì)云期待與你一起探索,助力你在 Linux 運(yùn)維之路上穩(wěn)步前行。





提交成功!非常感謝您的反饋,我們會繼續(xù)努力做到更好!

這條文檔是否有幫助解決問題?

非常抱歉未能幫助到您。為了給您提供更好的服務(wù),我們很需要您進(jìn)一步的反饋信息:

在文檔使用中是否遇到以下問題: