成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

使用HTTPie進行API測試

開發 開發工具 后端
HTTPie 是一個非常易用、易于升級的 HTTP 客戶端。它的發音為 “aitch-tee-tee-pie” 并以 http 命令運行,它是一個用 Python 編寫的來用于訪問 Web 的命令行工具。

[[276733]]

使用 HTTPie 調試 API,這是一個用 Python 寫的易用的命令行工具。

HTTPie 是一個非常易用、易于升級的 HTTP 客戶端。它的發音為 “aitch-tee-tee-pie” 并以 http 命令運行,它是一個用 Python 編寫的來用于訪問 Web 的命令行工具。

由于這是一篇關于 HTTP 客戶端的指導文章,因此你需要一個 HTTP 服務器來試用它。在這里,訪問 httpbin.org,它是一個簡單的開源 HTTP 請求和響應服務。httpbin.org 網站是一種測試 Web API 的強大方式,并能仔細管理并顯示請求和響應內容,不過現在讓我們專注于 HTTPie 的強大功能。

Wget 和 cURL 的替代品

你可能聽說過古老的 Wget 或稍微新一些的 cURL 工具,它們允許你從命令行訪問 Web。它們是為訪問網站而編寫的,而 HTTPie 則用于訪問 Web API。

網站請求發生在計算機和正在閱讀并響應它所看到的內容的最終用戶之間,這并不太依賴于結構化的響應。但是,API 請求會在兩臺計算機之間進行結構化調用,人并不是該流程內的一部分,像 HTTPie 這樣的命令行工具的參數可以有效地處理這個問題。

安裝 HTTPie

有幾種方法可以安裝 HTTPie。你可以通過包管理器安裝,無論你使用的是 brew、apt、yum 還是 dnf。但是,如果你已配置 virtualenvwrapper,那么你可以用自己的方式安裝:

  1. $ mkvirtualenv httpie
  2. ...
  3. (httpie) $ pip install httpie
  4. ...
  5. (httpie) $ deactivate
  6. $ alias http=~/.virtualenvs/httpie/bin/http
  7. $ http -b GET https://httpbin.org/get
  8. {
  9. "args": {},
  10. "headers": {
  11. "Accept": "*/*",
  12. "Accept-Encoding": "gzip, deflate",
  13. "Host": "httpbin.org",
  14. "User-Agent": "HTTPie/1.0.2"
  15. },
  16. "origin": "104.220.242.210, 104.220.242.210",
  17. "url": "https://httpbin.org/get"
  18. }

通過將 http 別名指向為虛擬環境中的命令,即使虛擬環境在非活動狀態,你也可以運行它。你可以將 alias 命令放在 .bash_profile.bashrc 中,這樣你就可以使用以下命令升級 HTTPie:

  1. $ ~/.virtualenvs/httpie/bin/pip install -U pip

使用 HTTPie 查詢網站

HTTPie 可以簡化查詢和測試 API。上面使用了一個選項,-b(即 --body)。沒有它,HTTPie 將默認打印整個響應,包括響應頭:

  1. $ http GET https://httpbin.org/get
  2. HTTP/1.1 200 OK
  3. Access-Control-Allow-Credentials: true
  4. Access-Control-Allow-Origin: *
  5. Connection: keep-alive
  6. Content-Encoding: gzip
  7. Content-Length: 177
  8. Content-Type: application/json
  9. Date: Fri, 09 Aug 2019 20:19:47 GMT
  10. Referrer-Policy: no-referrer-when-downgrade
  11. Server: nginx
  12. X-Content-Type-Options: nosniff
  13. X-Frame-Options: DENY
  14. X-XSS-Protection: 1; mode=block
  15.  
  16. {
  17. "args": {},
  18. "headers": {
  19. "Accept": "*/*",
  20. "Accept-Encoding": "gzip, deflate",
  21. "Host": "httpbin.org",
  22. "User-Agent": "HTTPie/1.0.2"
  23. },
  24. "origin": "104.220.242.210, 104.220.242.210",
  25. "url": "https://httpbin.org/get"
  26. }

這在調試 API 服務時非常重要,因為大量信息在響應頭中發送。例如,查看發送的 cookie 通常很重要。httpbin.org 提供了通過 URL 路徑設置 cookie(用于測試目的)的方式。以下設置一個標題為 opensource, 值為 awesome 的 cookie:

  1. $ http GET https://httpbin.org/cookies/set/opensource/awesome
  2. HTTP/1.1 302 FOUND
  3. Access-Control-Allow-Credentials: true
  4. Access-Control-Allow-Origin: *
  5. Connection: keep-alive
  6. Content-Length: 223
  7. Content-Type: text/html; charset=utf-8
  8. Date: Fri, 09 Aug 2019 20:22:39 GMT
  9. Location: /cookies
  10. Referrer-Policy: no-referrer-when-downgrade
  11. Server: nginx
  12. Set-Cookie: opensource=awesome; Path=/
  13. X-Content-Type-Options: nosniff
  14. X-Frame-Options: DENY
  15. X-XSS-Protection: 1; mode=block
  16.  
  17. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
  18. <title>Redirecting...</title>
  19. <h1>Redirecting...</h1>
  20. <p>You should be redirected automatically to target URL:
  21. <a href="/cookies">/cookies</a>. If not click the link.

注意 Set-Cookie: opensource=awesome; Path=/ 的響應頭。這表明你預期設置的 cookie 已正確設置,路徑為 /。另請注意,即使你得到了 302 重定向,http 也不會遵循它。如果你想要遵循重定向,則需要明確使用 --follow 標志請求:

  1. $ http --follow GET https://httpbin.org/cookies/set/opensource/awesome
  2. HTTP/1.1 200 OK
  3. Access-Control-Allow-Credentials: true
  4. Access-Control-Allow-Origin: *
  5. Connection: keep-alive
  6. Content-Encoding: gzip
  7. Content-Length: 66
  8. Content-Type: application/json
  9. Date: Sat, 10 Aug 2019 01:33:34 GMT
  10. Referrer-Policy: no-referrer-when-downgrade
  11. Server: nginx
  12. X-Content-Type-Options: nosniff
  13. X-Frame-Options: DENY
  14. X-XSS-Protection: 1; mode=block
  15.  
  16. {
  17. "cookies": {
  18. "opensource": "awesome"
  19. }
  20. }

但此時你無法看到原來的 Set-Cookie 頭。為了看到中間響應,你需要使用 --all

  1. $ http --headers --all --follow GET https://httpbin.org/cookies/set/opensource/awesome
  2. HTTP/1.1 302 FOUND
  3. Access-Control-Allow-Credentials: true
  4. Access-Control-Allow-Origin: *
  5. Content-Type: text/html; charset=utf-8
  6. Date: Sat, 10 Aug 2019 01:38:40 GMT
  7. Location: /cookies
  8. Referrer-Policy: no-referrer-when-downgrade
  9. Server: nginx
  10. Set-Cookie: opensource=awesome; Path=/
  11. X-Content-Type-Options: nosniff
  12. X-Frame-Options: DENY
  13. X-XSS-Protection: 1; mode=block
  14. Content-Length: 223
  15. Connection: keep-alive
  16.  
  17. HTTP/1.1 200 OK
  18. Access-Control-Allow-Credentials: true
  19. Access-Control-Allow-Origin: *
  20. Content-Encoding: gzip
  21. Content-Type: application/json
  22. Date: Sat, 10 Aug 2019 01:38:41 GMT
  23. Referrer-Policy: no-referrer-when-downgrade
  24. Server: nginx
  25. X-Content-Type-Options: nosniff
  26. X-Frame-Options: DENY
  27. X-XSS-Protection: 1; mode=block
  28. Content-Length: 66
  29. Connection: keep-alive

打印響應體并不有趣,因為你大多數時候只關心 cookie。如果你想看到中間請求的響應頭,而不是最終請求中的響應體,你可以使用:

  1. $ http --print hb --history-print h --all --follow GET https://httpbin.org/cookies/set/opensource/awesome
  2. HTTP/1.1 302 FOUND
  3. Access-Control-Allow-Credentials: true
  4. Access-Control-Allow-Origin: *
  5. Content-Type: text/html; charset=utf-8
  6. Date: Sat, 10 Aug 2019 01:40:56 GMT
  7. Location: /cookies
  8. Referrer-Policy: no-referrer-when-downgrade
  9. Server: nginx
  10. Set-Cookie: opensource=awesome; Path=/
  11. X-Content-Type-Options: nosniff
  12. X-Frame-Options: DENY
  13. X-XSS-Protection: 1; mode=block
  14. Content-Length: 223
  15. Connection: keep-alive
  16.  
  17. HTTP/1.1 200 OK
  18. Access-Control-Allow-Credentials: true
  19. Access-Control-Allow-Origin: *
  20. Content-Encoding: gzip
  21. Content-Type: application/json
  22. Date: Sat, 10 Aug 2019 01:40:56 GMT
  23. Referrer-Policy: no-referrer-when-downgrade
  24. Server: nginx
  25. X-Content-Type-Options: nosniff
  26. X-Frame-Options: DENY
  27. X-XSS-Protection: 1; mode=block
  28. Content-Length: 66
  29. Connection: keep-alive
  30.  
  31. {
  32. "cookies": {
  33. "opensource": "awesome"
  34. }
  35. }

你可以使用 --print 精確控制打印的內容(h:響應頭;b:響應體),并使用 --history-print 覆蓋中間請求的打印內容設置。

使用 HTTPie 下載二進制文件

有時響應體并不是文本形式,它需要發送到可被不同應用打開的文件:

  1. $ http GET https://httpbin.org/image/jpeg
  2. HTTP/1.1 200 OK
  3. Access-Control-Allow-Credentials: true
  4. Access-Control-Allow-Origin: *
  5. Connection: keep-alive
  6. Content-Length: 35588
  7. Content-Type: image/jpeg
  8. Date: Fri, 09 Aug 2019 20:25:49 GMT
  9. Referrer-Policy: no-referrer-when-downgrade
  10. Server: nginx
  11. X-Content-Type-Options: nosniff
  12. X-Frame-Options: DENY
  13. X-XSS-Protection: 1; mode=block
  14.  
  15.  
  16. +-----------------------------------------+
  17. | NOTE: binary data not shown in terminal |
  18. +-----------------------------------------+

要得到正確的圖片,你需要保存到文件:

  1. $ http --download GET https://httpbin.org/image/jpeg
  2. HTTP/1.1 200 OK
  3. Access-Control-Allow-Credentials: true
  4. Access-Control-Allow-Origin: *
  5. Connection: keep-alive
  6. Content-Length: 35588
  7. Content-Type: image/jpeg
  8. Date: Fri, 09 Aug 2019 20:28:13 GMT
  9. Referrer-Policy: no-referrer-when-downgrade
  10. Server: nginx
  11. X-Content-Type-Options: nosniff
  12. X-Frame-Options: DENY
  13. X-XSS-Protection: 1; mode=block
  14.  
  15. Downloading 34.75 kB to "jpeg.jpe"
  16. Done. 34.75 kB in 0.00068s (50.05 MB/s)

試一下!圖片很可愛。

使用 HTTPie 發送自定義請求

你可以發送指定的請求頭。這對于需要非標準頭的自定義 Web API 很有用:

  1. $ http GET https://httpbin.org/headers X-Open-Source-Com:Awesome
  2. {
  3. "headers": {
  4. "Accept": "*/*",
  5. "Accept-Encoding": "gzip, deflate",
  6. "Host": "httpbin.org",
  7. "User-Agent": "HTTPie/1.0.2",
  8. "X-Open-Source-Com": "Awesome"
  9. }
  10. }

最后,如果要發送 JSON 字段(盡管可以指定確切的內容),對于許多嵌套較少的輸入,你可以使用快捷方式:

  1. $ http --body PUT https://httpbin.org/anything open-source=awesome author=moshez
  2. {
  3. "args": {},
  4. "data": "{\"open-source\": \"awesome\", \"author\": \"moshez\"}",
  5. "files": {},
  6. "form": {},
  7. "headers": {
  8. "Accept": "application/json, */*",
  9. "Accept-Encoding": "gzip, deflate",
  10. "Content-Length": "46",
  11. "Content-Type": "application/json",
  12. "Host": "httpbin.org",
  13. "User-Agent": "HTTPie/1.0.2"
  14. },
  15. "json": {
  16. "author": "moshez",
  17. "open-source": "awesome"
  18. },
  19. "method": "PUT",
  20. "origin": "73.162.254.113, 73.162.254.113",
  21. "url": "https://httpbin.org/anything"
  22. }

下次在調試 Web API 時,無論是你自己的還是別人的,記得放下 cURL,試試 HTTPie 這個命令行工具。

責任編輯:龐桂玉 來源: Linux中國
相關推薦

2023-10-11 10:19:18

HTTPie桌面工具

2025-01-27 11:52:23

2013-06-04 09:49:04

Spring單元測試軟件測試

2013-01-18 10:31:20

JMeterHTTP負載

2021-03-28 23:03:50

Python程序員編碼

2021-07-03 08:54:49

LinuxSysbench性能

2021-08-05 11:30:49

Linux滲透測試

2009-05-20 14:43:38

ibmdwEasyMock測試

2013-06-13 10:29:39

CasperJS測試UI測試

2021-04-26 05:33:54

Python異步編程

2022-02-17 18:21:47

工具HTTPie客戶端

2020-11-05 18:30:32

接口測試

2024-11-21 15:24:49

2010-12-27 09:19:23

2021-11-29 22:59:34

Go Dockertest集成

2023-08-02 13:59:00

GoogleTestCTest單元測試

2021-12-27 10:46:07

WebAPIserver簽名

2017-01-16 12:12:29

單元測試JUnit

2017-01-14 23:26:17

單元測試JUnit測試

2018-01-02 15:38:52

WinSAT系統評估
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 黄色片网站国产 | 午夜三区 | 伊人久久免费视频 | 久久精品国产99国产精品 | 欧美毛片免费观看 | 日韩国产一区二区三区 | 久久国产欧美日韩精品 | 免费在线观看一区二区 | 亚洲国产成人精品女人久久久 | 日韩精品一区二区三区视频播放 | 成人在线h | 日韩中文在线观看 | 成人伊人 | 罗宾被扒开腿做同人网站 | 国产视频日韩 | 日日摸夜夜添夜夜添特色大片 | 久久专区| 欧美精品成人影院 | 97超级碰碰 | 国产三级网站 | 国产精品一区二区不卡 | 亚洲国产成人精品女人 | 亚洲成人午夜电影 | h视频在线免费观看 | 久久久久国产一区二区三区 | 国产一区二区视频免费在线观看 | 色性av| 久久精品成人热国产成 | 日韩在线观看一区 | 精品91视频 | 91视频免费视频 | a免费观看 | 免费一区| 国产精品一区一区 | 精品久久久久久中文字幕 | 国产精品视频入口 | 日韩一区二区三区av | 91精品国产高清一区二区三区 | 久久伊人影院 | 91大神在线看 | 特黄特色大片免费视频观看 |