API接口測試工具httpie

在使用 curl 測試API的過程中,發(fā)現(xiàn)量一些不方便或者可以說是繁瑣,畢竟編寫大量的命令行不是件輕松的事。
比如這個命令行

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

上面的命令行不僅僅是太長了,并且由于表單中包含特殊字符 !,無法得到正確的結(jié)果。

curl 是個歷史久遠的linux常用軟件,在linux,unix上被廣泛使用。我還是需要一個更加人性化的工具,比如 httpie。
HTTPie 是一個 HTTP 的命令行客戶端,目標是讓 CLI 和 web 服務(wù)之間的交互盡可能的人性化。這個工具提供了簡潔的 http 命令,允許通過自然的語法發(fā)送任意 HTTP 請求數(shù)據(jù),展示色彩化的輸出。HTTPie 可用于與 HTTP 服務(wù)器做測試、調(diào)試和常規(guī)交互。

上面的命令改成Httpie語法就成下面的樣子了:

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

主要特性:

- 直觀的語法
- 格式化和色彩化的終端輸出
- 內(nèi)置 JSON 支持
- 支持上傳表單和文件
- HTTPS、代理和認證
- 任意請求數(shù)據(jù)
- 自定義頭部
- 持久性會話
- 類 Wget 下載
- 支持 Python 2.6, 2.7 和 3.x
- 支持 Linux, Mac OS X 和 Windows
- 插件
- 文檔
- 測試覆蓋率

HTTPie 是用 Python 編寫,用到了 RequestsPygments 這些出色的庫。

Githubhttps://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 是一個現(xiàn)代的,通用的 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數(shù)據(jù)請求(默認就是JSON數(shù)據(jù)請求)

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

如果JSON數(shù)據(jù)存在不是字符串則用:=分隔,例如

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:pass@217.107.197.174:8081 proxy.demo.com
http --proxy=https:http://112.114.96.34:8118 proxy.demo.com
http --proxy=https:http://user:pass@112.114.96.34:8118 proxy.demo.com

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容