HTTPie - API测试工具的另一个选择

淩亂°似流年 2022-10-16 04:52 196阅读 0赞

文章目录

  • HTTPie - API测试工具的另一个选择
    • 安装HTTPie
    • HTTPie用法
    • IntelliJ HTTP Client plugin
    • 小结
    • 参考文档

HTTPie - API测试工具的另一个选择

可以使用HTTPie作为替代curl和Postman的API的测试工具。

安装HTTPie

MacOS上安装HTTPie:

  1. brew install httpie

HTTPie用法

HTTPie的设计哲学是尽可能的简洁。

  1. # help doc
  2. http --help
  3. # command syntax
  4. http [flags] [METHOD] URL [REQUEST_ITERM]
  5. # method
  6. # 没有发送数据时默认为GET
  7. http GET http://localhost:8080/coffees
  8. http http://localhost:8080/coffees
  9. # 有发送数据时默认为POST
  10. http POST http://localhost:8080/coffees id="123" name="Cafe Milk"
  11. http http://localhost:8080/coffees id="123" name="Cafe Milk"
  12. http http://localhost:8080/coffees < coffee.json
  13. echo '{ "id": "123", "name": "Cafe Happy" }' | http http://localhost:8080/coffees
  14. # 发送表单数据
  15. http -f :8080/kafka/publish message="hello"
  16. # URL
  17. # 默认以http://,也可以指定 https://
  18. # localhost可以缩写为':',比如localhost:8080可以缩写为:8080,而localhost:80可以缩写为:
  19. http http://localhost:8080/coffees
  20. http :8080/coffees
  21. http http://localhost:8080/coffees id="123" name="Cafe Milk"
  22. http :8080/coffees id="123" name="Cafe Milk"
  23. # REQUEST_ITEM
  24. # 可以用key-value pair来表示HTTP headers, parameters, json data, non-string json data, file, etc.
  25. # 注意key和value之间除了符号,不允许有空格
  26. # ':' HTTP headers:
  27. Referer:http://httpie.org Cookie:foo=bar User-Agent:bacon/1.0
  28. # '==' URL parameters to be appended to the request URI:
  29. search==httpie
  30. # '=' Data fields to be serialized into a JSON object (with --json, -j) or form data (with --form, -f):
  31. name=HTTPie language=Python description='CLI HTTP client'
  32. # 相当于JSON
  33. {
  34. "name": "HTTPie",
  35. "language": "Python",
  36. "description": "CLI HTTP client"
  37. }
  38. # ':=' Non-string JSON data fields (only with --json, -j):
  39. awesome:=true amount:=42 colors:='["red", "green", "blue"]'
  40. # 相当于JSON
  41. {
  42. "awesome": true,
  43. "amount": 42,
  44. "color": ["red", "green", "blue"]
  45. }
  46. # flags
  47. # Predefined Content Types
  48. # 数据格式
  49. # --json, -j,默认为JSON格式,Content-Type: application/json
  50. # --form, -f,表单格式,Content-Type: application/x-www-form-urlencoded
  51. # --multipart,Content-Type: multipart/form-data
  52. # Output Options
  53. # 默认只输出response headers和resonse body
  54. # --verbose, -v 输出详细信息
  55. # --quiet, -q 不输出信息
  56. # Authentication
  57. # 用户认证
  58. # --auth USER[:PASS], -a USER[:PASS] , 不提供密码时,HTTPie会要求输入密码
  59. # --auth-type {basic,digest}, -A {basic,digest},默认认证方法为basic,也就是用户名密码认证方式
  60. http -a USERNAME POST https://api.github.com/repos/httpie/httpie/issues/83/comments body='HTTPie is awesome! :heart:'
  61. # Network
  62. # --offline, Dry run, 不发送数据到站点
  63. http --offline :8080/coffees id="12345" name="Cafe Holiday"
  64. http --offline :8080/coffees id="12345" name="Cafe Holiday" > holiday.http

IntelliJ HTTP Client plugin

IntelliJ默认集成了HTTP Client插件 (基于HTTPie),可以用来作API测试。

HTTP Client插件支持编写HTTP测试脚本,也支持变量和断言,可以满足基本的API测试需要。

HTTP Client测试脚本示例:

  1. # API Testiing
  2. GET { { HOST}}/coffees
  3. Content-Type: application/json
  4. Connection: keep-alive
  5. > { %
  6. client.test("List Coffees successfully", function() {
  7. client.assert(response.status === 200, "Response status is not 200");
  8. });
  9. %}
  10. ###
  11. POST { { HOST}}/coffees
  12. Content-Type: application/json
  13. Connection: keep-alive
  14. {
  15. "id": "123",
  16. "name": "Coffee Happy"
  17. }
  18. > { %
  19. client.test("Add Coffee successfully", function() {
  20. client.assert(response.status === 200, "Response status is not 200");
  21. });
  22. %}
  23. ###

小结

在使用命令行时,用HTTPie比curl更简单方便。

在有界面时,用Postman更为简单高效,但是不想用另外一个工具或切换另一个窗口,可以直接使用IntelliJ里集成的HTTP Client插件。

但是IntelliJ HTTP Client的测试脚本只能在IntelliJ中运行,也不能集成到CI流水线中,而Postman可以通过Newman来集成到CI流水线中。

另外如果在单元测试代码中对API进行测试,Restassured可能是更好的选择。

参考文档

参考文档:

  • https://httpie.io/docs
  • https://www.jetbrains.com/help/idea/testing-restful-web-services.html
  • How to test API Using Jetbrains IntelliJ HTTP Client
  • HTTP Client in PhpStorm Overview
  • HTTPie vs. Postman
  • 用Rest assured作API自动化集成测试

发表评论

表情:
评论列表 (有 0 条评论,196人围观)

还没有评论,来说两句吧...

相关阅读

    相关 httpie 基本操作

    HTTPie (读aych-tee-tee-pie)是一个 HTTP 的命令行客户端。其目标是让 CLI 和 web 服务之间的交互尽可能的人性化。 其有特点如下:

    相关 httpie 工具使用

    HTTPIE 工具使用入门 HTTPie 是一个 HTTP 的命令行客户端,目标是让 CLI 和 web 服务之间的交互尽可能的人性化。这个工具提 供了