API接口测试工具httpie

在使用 curl 测试API的过程中,发现量一些不方便或者可以说是繁琐,毕竟编写大量的命令行不是件轻松的事。
比如这个命令行

curl -X POST --header "Content-Type: application/x-www-form-urlencoded" --data "client_id=client1&client_secret=client1secret&grant_type=password&scope=openid&username=demo&password=!Demo123" localhost:5000/connect/token

上面的命令行不仅仅是太长了,并且由于表单中包含特殊字符 !,无法得到正确的结果。

curl 是个历史久远的linux常用软件,在linux,unix上被广泛使用。我还是需要一个更加人性化的工具,比如 httpie。
HTTPie 是一个 HTTP 的命令行客户端,目标是让 CLI 和 web 服务之间的交互尽可能的人性化。这个工具提供了简洁的 http 命令,允许通过自然的语法发送任意 HTTP 请求数据,展示色彩化的输出。HTTPie 可用于与 HTTP 服务器做测试、调试和常规交互。

上面的命令改成Httpie语法就成下面的样子了:

http -f POST localhost:5000/connect/token client_id='client1' client_secret='client1secret' grant_type='password' scope='openid' username='demo' password='!Demo123'

主要特性:

- 直观的语法
- 格式化和色彩化的终端输出
- 内置 JSON 支持
- 支持上传表单和文件
- HTTPS、代理和认证
- 任意请求数据
- 自定义头部
- 持久性会话
- 类 Wget 下载
- 支持 Python 2.6, 2.7 和 3.x
- 支持 Linux, Mac OS X 和 Windows
- 插件
- 文档
- 测试覆盖率

HTTPie 是用 Python 编写,用到了 Requests 和 Pygments 这些出色的库。

Github:https://github.com/jakubroztocil/httpie

1. 安装 httpie

httpie 是跨平台命令,支持 Mac OS X、 Linux、 Windows

1) Mac OS X

brew install httpie        // brew 命令安装
port install httpie          // ports 命令安装

2) Linux

easy_install httpie  (CentOS 5.6/6.5/7.2 都成功)

# Debian, Ubuntu, etc.
apt-get install httpie

# Fedora, CentOS, RHEL, …
yum install httpie

# Arch Linux
pacman -S httpie

3)Windows

# Make sure we have an up-to-date version of pip and setuptools:
$ pip install --upgrade pip setuptools
$ pip install --upgrade httpie

pip 是一个现代的,通用的 Python 包管理工具。提供了对Python 包的查找、下载、安装、卸载的功能。一般安装Python的时候默认安装了pip。

2. httpie 帮助

# http
usage: http [--json] [--form] [--pretty {all,colors,format,none}]
            [--style STYLE] [--print WHAT] [--headers] [--body] [--verbose]
            [--all] [--history-print WHAT] [--stream] [--output FILE]
            [--download] [--continue]
            [--session SESSION_NAME_OR_PATH | --session-read-only SESSION_NAME_OR_PATH]
            [--auth USER[:PASS]] [--auth-type {basic,digest}]
            [--proxy PROTOCOL:PROXY_URL] [--follow]
            [--max-redirects MAX_REDIRECTS] [--timeout SECONDS]
            [--check-status] [--verify VERIFY]
            [--ssl {ssl2.3,ssl3,tls1,tls1.1,tls1.2}] [--cert CERT]
            [--cert-key CERT_KEY] [--ignore-stdin] [--help] [--version]
            [--traceback] [--default-scheme DEFAULT_SCHEME] [--debug]
            [METHOD] URL [REQUEST_ITEM [REQUEST_ITEM ...]]
http: error: the following arguments are required: URL

3. httpie 命令

显示请求信息(包含返回头200)

http demo.com

显示详细的请求(包含请求和返回头200)

http -v demo.com

只显示Header

http -h demo.com
http --head demo.com
http --header demo.com
http --headers demo.com

只显示Body

http -b demo.com
http --body demo.com

下载文件

http -d demo.com

模拟提交表单

http -f POST demo.com username='demo-user'

请求删除的方法

http DELETE demo.com

传递JSON数据请求(默认就是JSON数据请求)

http PUT demo.com username='demo-user' password='demo-pwd'

如果JSON数据存在不是字符串则用:=分隔,例如

http PUT demo.com username='demo-user' password='demo-pwd' age:=28 a:=true streets:='["a", "b"]'

模拟Form的Post请求, Content-Type: application/x-www-form-urlencoded; charset=utf-8

http --form POST demo.com username='demo-user'

模拟Form的上传, Content-Type: multipart/form-data

http -f POST example.com/jobs username='demo-user' file@~/test.pdf

修改请求头, 使用:分隔

http demo.com  User-Agent:demo-agent/1.0  'Cookie:a=b;b=c'  Referer:http://demo.com/

一些特殊的请求头,比如 Authorization:Bearer eyJhbGciOiJSUzI1 ,需要单引号

http localhost:5500/api2/cityweather 'Authorization:Bearer eyJhbGciOiJSUzI1NiIs'

认证

http -a username:password demo.com
http --auth-type=digest -a username:password demo.com

使用http代理

http --proxy=http:http://217.107.197.174:8081 proxy.demo.com
http --proxy=http:http://user:[email protected]:8081 proxy.demo.com
http --proxy=https:http://112.114.96.34:8118 proxy.demo.com
http --proxy=https:http://user:[email protected]:8118 proxy.demo.com

其他参考:
cURL vs HTTPie on the Command Line for HTTP APIs

你可能感兴趣的:(API接口测试工具httpie)