找回密碼
 註冊
搜索
查看: 603|回復: 0

[CentOS] Curl 指令與基本操作入門教學

[複製鏈接]
發表於 2024-9-25 11:05:41 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
Curl 指令基本介紹與常見用法
Curl 是一個在 Linux 上用來透過 HTTP Protocol(HTTP HyperText Transfer Protocol 定義存取網路資源的協定,讓我們可以使用 client / server 模式來取得網路資源)下載和上傳檔案的指令(比起 wget 只能下載強大許多)。它基本的指令格式如下:

  1. curl [options] [URL...]
複製代碼

新手上路,我們來看看最基本的用法:

打開終端機(terminal)然後,curl 後面加網址,就會在終端機內顯示回傳的 response,可能是 HTML、JSON 或是 XML 等格式,根據輸入的 URL 內容而定。
  1. curl https://www.google.com
複製代碼


例如我想要下載:黃色小鴨的圖片,可以使用 -o 搭配欲下載的檔名和網址
  1. curl -o duck.jpg https://im2.book.com.tw/image/getImage?i=https://www.books.com.twhttps://static.coderbridge.com/img/techbridge/images/N00/040/56/N000405619.jpg&v=522ff1cf&w=348&h=348
複製代碼


若是使用 -O 則可以直接使用下載網址的檔案檔名來命名下載的檔案(N000405619.jpg):
  1. curl -O https://im2.book.com.tw/image/getImage?i=https://www.books.com.twhttps://static.coderbridge.com/img/techbridge/images/N00/040/56/N000405619.jpg&v=522ff1cf&w=348&h=348
複製代碼


有可能在下載過程中被中斷,若是想要從中斷的地方繼續的話,可以使用 -C 選項:
  1. curl -C - -O http://releases.ubuntu.com/18.04/ubuntu-18.04-desktop-amd64.iso
複製代碼


若希望可以跟隨著網址 301/302 redirect 的話,可以使用 -L 選項:
  1. curl -L http://google.com
複製代碼


可以比較沒有使用 -L 的回應:
  1. $ curl http://google.com
  2. <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
  3. <TITLE>301 Moved</TITLE></HEAD><BODY>
  4. <H1>301 Moved</H1>
  5. The document has moved
  6. <A HREF="http://www.google.com/">here</A>.
  7. </BODY></HTML>
複製代碼


若我們要追蹤整個 curl 過程?並將結果存入 debugdump.txt 檔案可以使用 --trace-ascii 指令:
  1. curl --trace-ascii debugdump.txt http://www.example.com/
  2. $ cat debugdump.txt
  3. == Info:   Trying 93.184.216.34...
  4. == Info: TCP_NODELAY set
  5. == Info: Connected to www.example.com (93.184.216.34) port 80 (#0)
  6. => Send header, 142 bytes (0x8e)
  7. 0000: GET / HTTP/1.1
  8. 0010: Host: www.example.com
  9. 0027: User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; T
  10. 0067: rident/5.0)
  11. 0074: Accept: */*
  12. 0081: Referer:
  13. 008c:
  14. <= Recv header, 17 bytes (0x11)
  15. 0000: HTTP/1.1 200 OK
  16. <= Recv header, 31 bytes (0x1f)
  17. 0000: Cache-Control: max-age=604800
  18. <= Recv header, 40 bytes (0x28)
  19. 0000: Content-Type: text/html; charset=UTF-8
  20. <= Recv header, 37 bytes (0x25)
  21. 0000: Date: Sat, 02 Feb 2019 13:54:23 GMT
  22. <= Recv header, 31 bytes (0x1f)
  23. 0000: Etag: "1541025663+gzip+ident"
  24. <= Recv header, 40 bytes (0x28)
  25. 0000: Expires: Sat, 09 Feb 2019 13:54:23 GMT
  26. <= Recv header, 46 bytes (0x2e)
  27. 0000: Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
  28. <= Recv header, 24 bytes (0x18)
  29. 0000: Server: ECS (sjc/4E44)
  30. <= Recv header, 23 bytes (0x17)
  31. 0000: Vary: Accept-Encoding
  32. <= Recv header, 14 bytes (0xe)
  33. 0000: X-Cache: HIT
  34. <= Recv header, 22 bytes (0x16)
  35. 0000: Content-Length: 1270
  36. <= Recv header, 2 bytes (0x2)
  37. 0000:
  38. <= Recv data, 1270 bytes (0x4f6)
  39. 0000: <!doctype html>.<html>.<head>.    <title>Example Domain</title>.
  40. 0040: .    <meta charset="utf-8" />.    <meta http-equiv="Content-type
  41. 0080: " content="text/html; charset=utf-8" />.    <meta name="viewport
  42. 00c0: " content="width=device-width, initial-scale=1" />.    <style ty
  43. 0100: pe="text/css">.    body {.        background-color: #f0f0f2;.
  44. 0140:      margin: 0;.        padding: 0;.        font-family: "Open S
  45. 0180: ans", "Helvetica Neue", Helvetica, Arial, sans-serif;.        .
  46. 01c0:    }.    div {.        width: 600px;.        margin: 5em auto;.
  47. 0200:        padding: 50px;.        background-color: #fff;.        bo
  48. 0240: rder-radius: 1em;.    }.    a:link, a:visited {.        color: #
  49. 0280: 38488f;.        text-decoration: none;.    }.    @media (max-wid
  50. 02c0: th: 700px) {.        body {.            background-color: #fff;.
  51. 0300:         }.        div {.            width: auto;.            mar
  52. 0340: gin: 0 auto;.            border-radius: 0;.            padding:
  53. 0380: 1em;.        }.    }.    </style>    .</head>..<body>.<div>.
  54. 03c0: <h1>Example Domain</h1>.    <p>This domain is established to be
  55. 0400: used for illustrative examples in documents. You may use this.
  56. 0440:   domain in examples without prior coordination or asking for pe
  57. 0480: rmission.</p>.    <p><a href="http://www.iana.org/domains/exampl
  58. 04c0: e">More information...</a></p>.</div>.</body>.</html>.
  59. == Info: Curl_http_done: called premature == 0
  60. == Info: Connection #0 to host www.example.com left intact
複製代碼


使用 Curl 來進行 HTTP Request
除了簡易的下載檔案或是取得網頁內容外,Curl 還支援各種不同 HTTP 請求方法(HTTP method),以下列出常用指令和選項參數:

-X/--request [GET|POST|PUT|DELETE|PATCH]  使用指定的 http method 來發出 http request
-H/--header                           設定 request 裡所攜帶的 header
-i/--include                          在 output 顯示 response 的 header
-d/--data                             攜帶 HTTP POST Data
-v/--verbose                          輸出更多的訊息方便 debug
-u/--user                             攜帶使用者帳號、密碼
-b/--cookie                           攜帶 cookie(可以是參數或是檔案位置)
GET
簡易一個 URL 版本,可以攜帶 query string 參數取得網路資源:
  1. $ curl https://example.com?q1=123&q2=abc
複製代碼


在同一個指令使用多個 URL:

  1. $ curl http://example1.com http://example2.com
複製代碼

Form POST
一般而言我們 Form 表單的 HTML 會長這樣:

  1. <form method="POST" action="form.php">
  2.     <input type=text name="email">
  3.     <input type=submit name=press value=" OK ">
  4. </form>
複製代碼

由於 Form post 是使用 application/x-www-form-urlencoded Content-Type,所以傳遞的值需要編碼:

  1. $ curl -X POST --data "email=test@example.com&press=%20OK%20"  http://www.example.com/form.php
複製代碼

File Upload POST
另一種常見 Form 表單是有涉及檔案上傳(使用 multipart/form-data Content-Type):
  1. <form method="POST" enctype='multipart/form-data' action="upload.php">
  2. <input type=file name=upload>
  3. <input type=submit name=press value="OK">
  4. </form>
複製代碼

可以看到指令會需要攜帶 upload 檔案:

  1. $ curl -X POST -F 'file=@./upload.txt' http://www.example.com/upload.php
複製代碼

常見 Restful CRUD 指令:

GET 單一和全部資源

  1. $ curl -X GET "http://www.example.com/api/resources"
  2. $ curl -X GET "http://www.example.com/api/resources/1"
複製代碼

POST JSON 資料:

  1. $ curl -X POST -H "Content-Type: application/json" -d '{"status" : false, "name" : "Jack"}' "http://www.example.com/api/resources"
複製代碼

PUT JSON 資料:

  1. $ curl -X PUT -H "Content-Type: application/json" -d '{"status" : false }' "http://www.example.com/api/resources"
複製代碼

DELETE 資源:

  1. $ curl -X DELETE "http://www.example.com/api/resources/1"
複製代碼

攜帶 cookie

在指令中輸入 cookie:

  1. $ curl --cookie "name=Jack" http://www.example.com
複製代碼

從檔案讀取 cookie:

  1. $ curl --cookie stored_cookies_file_path http://www.example.com
複製代碼

攜帶 User Agent
$ curl --user-agent "Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)" http://www.example.com
Basic Authentication
若所存取網頁有使用 Basic Authentication,可以攜帶 --user username:password 來通過驗證:

  1. $ curl -i --user secret:vary_secret http://www.example.com/api/resources
複製代碼

總結
以上介紹了 Linux Curl 常用指令和基礎操作的入門教學介紹:

GET/POST/PUT/DELETE 操作
下載檔案
Form 表單操作
檔案上傳
Restful CRUD 指令
User Agent
Basic Authentication
之後讀者除了使用 Postman 等 GUI Tool 外,也可以使用 Curl 來進行 HTTP endpoint 的測試和開發上!讀者也可以分享自己的常用的指令來提升工作效率!


文章出處https://blog.techbridge.cc/2019/ ... l-command-tutorial/
 
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

Archiver|手機版|小黑屋|TShopping

GMT+8, 2025-4-30 16:37 , Processed in 0.023653 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回復 返回頂部 返回列表