其他分享
首页 > 其他分享> > HTTPie - API测试工具的另一个选择

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

作者:互联网

文章目录

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

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

安装HTTPie

MacOS上安装HTTPie:

bewq install httpie

HTTPie用法

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

# help doc
http --help

# command syntax
http [flags] [METHOD] URL [REQUEST_ITERM]

# method
# 没有发送数据时默认为GET
http GET http://localhost:8080/coffees
http http://localhost:8080/coffees

# 有发送数据时默认为POST
http POST http://localhost:8080/coffees id="123" name="Cafe Milk"
http http://localhost:8080/coffees id="123" name="Cafe Milk"
http http://localhost:8080/coffees < coffee.json
echo '{ "id": "123", "name": "Cafe Happy" }' | http http://localhost:8080/coffees


# URL
# 默认以http://,也可以指定 https://
# localhost可以缩写为':',比如localhost:8080可以缩写为:8080,而localhost:80可以缩写为:
http http://localhost:8080/coffees
http :8080/coffees

http http://localhost:8080/coffees id="123" name="Cafe Milk"
http :8080/coffees id="123" name="Cafe Milk"

# REQUEST_ITEM
# 可以用key-value pair来表示HTTP headers, parameters, json data, non-string json data, file, etc.
# 注意key和value之间除了符号,不允许有空格
# ':' HTTP headers:
Referer:http://httpie.org  Cookie:foo=bar  User-Agent:bacon/1.0
# '==' URL parameters to be appended to the request URI:
search==httpie
# '=' Data fields to be serialized into a JSON object (with --json, -j) or form data (with --form, -f):
name=HTTPie  language=Python  description='CLI HTTP client'

# 相当于JSON
{
  "name": "HTTPie",
  "language": "Python",
  "description": "CLI HTTP client"
}

# ':=' Non-string JSON data fields (only with --json, -j):
awesome:=true  amount:=42  colors:='["red", "green", "blue"]'

# 相当于JSON
{
  "awesome": true,
  "amount": 42,
  "color": ["red", "green", "blue"]
}

# flags

# Predefined Content Types
# 数据格式
# --json, -j,默认为JSON格式,Content-Type: application/json
# --form, -f,表单格式,Content-Type: application/x-www-form-urlencoded
# --multipart,Content-Type: multipart/form-data

# Output Options
# 默认只输出response headers和resonse body
# --verbose, -v 输出详细信息
# --quiet, -q 不输出信息


# Authentication
# 用户认证
# --auth USER[:PASS], -a USER[:PASS] , 不提供密码时,HTTPie会要求输入密码
# --auth-type {basic,digest}, -A {basic,digest},默认认证方法为basic,也就是用户名密码认证方式
http -a USERNAME POST https://api.github.com/repos/httpie/httpie/issues/83/comments body='HTTPie is awesome! :heart:'

# Network
# --offline, Dry run, 不发送数据到站点
http --offline :8080/coffees id="12345" name="Cafe Holiday"
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测试脚本示例:

# API Testiing
GET {{HOST}}/coffees
Content-Type: application/json
Connection: keep-alive

> {%
  client.test("List Coffees successfully", function() {
        client.assert(response.status === 200, "Response status is not 200");
    });
 %}

###

POST {{HOST}}/coffees
Content-Type: application/json
Connection: keep-alive

{
  "id": "123",
  "name": "Coffee Happy"
}

> {%
  client.test("Add Coffee successfully", function() {
        client.assert(response.status === 200, "Response status is not 200");
    });
 %}

###

小结

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

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

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

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

参考文档

参考文档:

标签:HTTP,http,--,HTTPie,API,测试工具,8080,coffees
来源: https://blog.csdn.net/nklinsirui/article/details/117307027