日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
使用HTTPie進行API測試

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

成都創(chuàng)新互聯(lián)公司是少有的成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、營銷型企業(yè)網(wǎng)站、微信小程序定制開發(fā)、手機APP,開發(fā)、制作、設(shè)計、友情鏈接、推廣優(yōu)化一站式服務(wù)網(wǎng)絡(luò)公司,于2013年成立,堅持透明化,價格低,無套路經(jīng)營理念。讓網(wǎng)頁驚喜每一位訪客多年來深受用戶好評

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

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

Wget 和 cURL 的替代品

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

網(wǎng)站請求發(fā)生在計算機和正在閱讀并響應(yīng)它所看到的內(nèi)容的最終用戶之間,這并不太依賴于結(jié)構(gòu)化的響應(yīng)。但是,API 請求會在兩臺計算機之間進行結(jié)構(gòu)化調(diào)用,人并不是該流程內(nèi)的一部分,像 HTTPie 這樣的命令行工具的參數(shù)可以有效地處理這個問題。

安裝 HTTPie

有幾種方法可以安裝 HTTPie。你可以通過包管理器安裝,無論你使用的是 brew、aptyum 還是 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 別名指向為虛擬環(huán)境中的命令,即使虛擬環(huán)境在非活動狀態(tài),你也可以運行它。你可以將 alias 命令放在 .bash_profile.bashrc 中,這樣你就可以使用以下命令升級 HTTPie:

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

使用 HTTPie 查詢網(wǎng)站

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

 
 
 
  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. "args": {},
  17. "headers": {
  18. "Accept": "*/*",
  19. "Accept-Encoding": "gzip, deflate",
  20. "Host": "httpbin.org",
  21. "User-Agent": "HTTPie/1.0.2"
  22. },
  23. "origin": "104.220.242.210, 104.220.242.210",
  24. "url": "https://httpbin.org/get"
  25. }

這在調(diào)試 API 服務(wù)時非常重要,因為大量信息在響應(yīng)頭中發(fā)送。例如,查看發(fā)送的 cookie 通常很重要。httpbin.org 提供了通過 URL 路徑設(shè)置 cookie(用于測試目的)的方式。以下設(shè)置一個標題為 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. Redirecting...
  17. Redirecting...

  18. You should be redirected automatically to target URL:

  19. /cookies. If not click the link.

注意 Set-Cookie: opensource=awesome; Path=/ 的響應(yīng)頭。這表明你預(yù)期設(shè)置的 cookie 已正確設(shè)置,路徑為 /。另請注意,即使你得到了 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. "cookies": {
  17. "opensource": "awesome"
  18. }
  19. }

但此時你無法看到原來的 Set-Cookie 頭。為了看到中間響應(yīng),你需要使用 --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. HTTP/1.1 200 OK
  17. Access-Control-Allow-Credentials: true
  18. Access-Control-Allow-Origin: *
  19. Content-Encoding: gzip
  20. Content-Type: application/json
  21. Date: Sat, 10 Aug 2019 01:38:41 GMT
  22. Referrer-Policy: no-referrer-when-downgrade
  23. Server: nginx
  24. X-Content-Type-Options: nosniff
  25. X-Frame-Options: DENY
  26. X-XSS-Protection: 1; mode=block
  27. Content-Length: 66
  28. Connection: keep-alive

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

 
 
 
  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. HTTP/1.1 200 OK
  17. Access-Control-Allow-Credentials: true
  18. Access-Control-Allow-Origin: *
  19. Content-Encoding: gzip
  20. Content-Type: application/json
  21. Date: Sat, 10 Aug 2019 01:40:56 GMT
  22. Referrer-Policy: no-referrer-when-downgrade
  23. Server: nginx
  24. X-Content-Type-Options: nosniff
  25. X-Frame-Options: DENY
  26. X-XSS-Protection: 1; mode=block
  27. Content-Length: 66
  28. Connection: keep-alive
  29. {
  30. "cookies": {
  31. "opensource": "awesome"
  32. }
  33. }

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

使用 HTTPie 下載二進制文件

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

 
 
 
  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. | NOTE: binary data not shown in terminal |
  16. +-----------------------------------------+

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

 
 
 
  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. Downloading 34.75 kB to "jpeg.jpe"
  15. Done. 34.75 kB in 0.00068s (50.05 MB/s)

試一下!圖片很可愛。

使用 HTTPie 發(fā)送自定義請求

你可以發(fā)送指定的請求頭。這對于需要非標準頭的自定義 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. }

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

 
 
 
  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. }

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


網(wǎng)頁標題:使用HTTPie進行API測試
文章位置:http://www.dlmjj.cn/article/djojheh.html