使用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。你可以通过包管理器安装,无论你使用的是 brewaptyum 还是 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中国
相关推荐

2013-01-18 10:31:20

JMeterHTTP负载

2013-06-04 09:49:04

Spring单元测试软件测试

2023-10-11 10:19:18

HTTPie桌面工具

2021-04-26 05:33:54

Python异步编程

2009-05-20 14:43:38

ibmdwEasyMock测试

2013-06-13 10:29:39

CasperJS测试UI测试

2021-07-03 08:54:49

LinuxSysbench性能

2021-03-28 23:03:50

Python程序员编码

2021-08-05 11:30:49

Linux渗透测试

2021-12-27 10:46:07

WebAPIserver签名

2021-11-29 22:59:34

Go Dockertest集成

2023-08-02 13:59:00

GoogleTestCTest单元测试

2020-11-05 18:30:32

接口测试

2010-12-27 09:19:23

2022-02-17 18:21:47

工具HTTPie客户端

2017-01-16 12:12:29

单元测试JUnit

2017-01-14 23:26:17

单元测试JUnit测试

2015-11-25 18:22:41

2023-10-07 08:49:56

测试驱动开发Xunit 框架

2022-06-08 07:36:03

LocustKubernete微服务
点赞
收藏

51CTO技术栈公众号