HTTPie 简介

HTTPie 简介

HTTPie(发音 aitch-tee-tee-pie)是一个命令行 HTTP 客户端。它的目的是使 CLI 和网络服务之间的交互尽可能人性化。它提供了一个简单的 HTTP 命令,允许使用简单自然的语法发送任意 HTTP 请求,并显示多彩输出。HTTPie 可用于测试,调试以及通常与 HTTP 服务器交互。

开始使用

HTTPie 的安装,访问官网 httpie.org 教程。

Hello World:

http httpie.org

基础语法:

http [flags] [METHOD] URL [ITEM [ITEM]]

使用举例

HTTPie 支持自定义 HTTP method, HTTP 请求头和发送请求数据,文件下载等:

http PUT example.com X-API-Token:123 name=John

提交表单数据:

http -f POST example.org hello=World

查看请求并使用输出选项参数:

http -v example.com

通过身份验证使用 Github API 提交一个 issue 评论:

http -a USERNAME POST https://api.github.com/repos/jakubroztocil/httpie/issues/83/comments body='HTTPie is awesome! :heart:'

使用重定向输入上传文件:

http example.org < file.json

通过重定向输出下载文件:

http example.org/file > file

也可以使用 wget 风格下载文件:

http --download example.org/file

设置自定义 Host 请求头防止丢失 DNS 记录:

http localhost:8000 Host:example.com

HTTP method

HTTP 请求方法的名称在命令的后面,URL 参数的前面:

http DELETE example.org/todos/7

当从命令行中删除 METHOD 参数时,HTTPie 设置默认值为 GET(没有请求体时)或者 POET(有请求体时)。

HTTP 请求方法有:GET, POST, HEAD, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH

关于 HTTP method,详见 MDN 的介绍。

HTTP 请求头

为了设置请求头你可以使用 Header:Value 标记:

http example.org  User-Agent:Bacon/1.0 'Cookie:valued-visitor=yes;foo=bar' \
    X-Foo:Bar Referer:http://httpie.org/

上面的命令结果是:

GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Cookie: valued-visitor=yes;foo=bar
Host: example.org
Referer: http://httpie.org/
User-Agent: Bacon/1.0
X-Foo: Bar

HTTPie 默认设置的请求头

HTTPie 设置的默认请求头,它们是一些值对:

GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
User-Agent: HTTPie/<version>
Host: <taken-from-URL>

除了 Host 以外的任何一个都可以重写覆盖,也可以取消设置。

空请求头和取消设置请求头

想要取消上面默认指定的请求头,可以使用 Header:

http httpbin.org/headers Accept: User-Agent:

也可以使用 Header: 来发送一个空值请求头:

http httpbin.org/headers 'Header;'

Cookies

HTTPie 使用通常的 Header:Value 来设置 HTTP 客户端发送到服务器的 cookies:

发送单个 cookie:

http example.org Cookie:sessionid=foo

对应的 HTTP 请求:

GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Cookie: sessionid=foo
Host: example.org
User-Agent: HTTPie/0.9.9

发送多个 cookies 值时,cookies 需要使用引号包裹并以 ; 分号分隔的值对:

http example.org 'Cookie:sessionid=foo;another-cookie=bar'

HTTP 请求将是:

GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Cookie: sessionid=foo;another-cookie=bar
Host: example.org
User-Agent: HTTPie/0.9.9

HTTPS

随着互联网对安全和隐私越来越重视,越来越多的网站开启了 HTTPS 访问。HTTPie 支持开启 HTTPS 请求。

服务端 SSL 证书认证

HTTPie 在请求 HTTPS 网站时默认开启服务器 SSL 证书认证。如果您想关闭,可以通过设置 --verify=no(默认值是 yes):

http --verify=no https://example.com

自定义 CA 捆绑包

可以通过设置 --verify=<CA_BUNDLE_PATH> 来自定义 HTTPS CA 包:

http --verify=/ssl/custom_ca_bundle https://example.com

客户端 SSL 证书

通过 --cert 参数传递 cert 文件在客户端完成 SSL 通信:

http --cert=client.pem https://example.com

同时可以使用 --cert-key 参数传递密钥文件:

http --cert=client.crt --cert-key=client.key https://example.com

自定义 SSL 版本

HTTPie 默认使用的 SSL 版本是 v2.3,使用 --ssl=<PROTOCOL> 参数指定要使用的版本。可接受的版本有 ssl2.3, ssl3, tls1, tls1.1, tls1.2, tls1.3

# 指定 SSL3 协议与服务器通信:
http --ssl=ssl3 https://vulnerable.example.com

更多 HTTPie 特色

HTTPie 提供了丰富的命令行发送 HTTP 请求,并在此基础上提高交互的人性化。相比 curl 压缩文字的输出,HTTPie 多彩且格式清晰的终端输出,阅读性和交互性更优秀。

  • 富有表现力和直观的语法
  • 格式化并多色彩的终端输出
  • 内置 JSON 支持
  • 支持表单和文件上传
  • HTTPS,代理和身份验证
  • 任意请求数据
  • 自定义请求头
  • 持久的 sessions 保持
  • 类似 Wget 的下载
  • 支持 Python 2.7 和 3.x
  • 支持 Linux,macOS 和 Windows
  • 丰富的插件
  • 易读的文档
  • 充分的测试

更多关于 HTTPie,可以访问 httpie.org 了解。

本文作者 月之,转载请注明来源链接:

本文链接:https://tie.pub/2019/09/httpie-guide/