大多数时间我们会使用 curl
命令或是 wget
命令下载文件或者做其他事。
今天我们将讨论同样的话题。这个实用程序名为 HTTPie。
它是现代命令行 http 客户端,也是 curl
和 wget
命令的最佳替代品。
HTTPie (发音是 aitch-tee-tee-pie) 是一个 HTTP 命令行客户端。
HTTPie 工具是现代的 HTTP 命令行客户端,它能通过命令行界面与 Web 服务进行交互。
它提供一个简单的 http
命令,允许使用简单而自然的语法发送任意的 HTTP 请求,并会显示彩色的输出。
HTTPie 能用于测试、调试及与 HTTP 服务器交互。
wget
的下载pip install httpie
大部分 Linux 发行版都提供了系统包管理器,可以用它来安装。
Fedora 系统:
$ sudo dnf install httpie
Debian/Ubuntu 系统
$ sudo apt install httpie
基于 Arch Linux 的系统
$ sudo pacman -S httpie
RHEL/CentOS 的系统
$ sudo yum install httpie
openSUSE Leap 系统
$ sudo zypper install httpie
HTTPie 的基本用法是将网站的 URL 作为参数。
# http 2daygeek.com HTTP/1.1 301 Moved Permanently CF-RAY: 4c4a618d0c02ce6d-LHR Cache-Control: max-age=3600 Connection: keep-alive Date: Tue, 09 Apr 2019 06:21:28 GMT Expires: Tue, 09 Apr 2019 07:21:28 GMT Location: https://2daygeek.com/ Server: cloudflare Transfer-Encoding: chunked Vary: Accept-Encoding
你可以使用带 --download
参数的 HTTPie 命令下载文件。类似于 wget
命令。
# http --download https://www.2daygeek.com/wp-content/uploads/2019/04/Anbox-Easy-Way-To-Run-Android-Apps-On-Linux.png HTTP/1.1 200 OK Accept-Ranges: bytes CF-Cache-Status: HIT CF-RAY: 4c4a65d5ca360a66-LHR Cache-Control: public, max-age=7200 Connection: keep-alive Content-Length: 32066 Content-Type: image/png Date: Tue, 09 Apr 2019 06:24:23 GMT Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" Expires: Tue, 09 Apr 2019 08:24:23 GMT Last-Modified: Mon, 08 Apr 2019 04:54:25 GMT Server: cloudflare Set-Cookie: __cfduid=dd2034b2f95ae42047e082f59f2b964f71554791063; expires=Wed, 08-Apr-20 06:24:23 GMT; path=/; domain=.2daygeek.com; HttpOnly; Secure Vary: Accept-Encoding Downloading 31.31 kB to "Anbox-Easy-Way-To-Run-Android-Apps-On-Linux.png" Done. 31.31 kB in 0.01187s (2.58 MB/s)
你还可以使用 -o
参数用不同的名称保存输出文件。
# http --download https://www.2daygeek.com/wp-content/uploads/2019/04/Anbox-Easy-Way-To-Run-Android-Apps-On-Linux.png -o Anbox-1.png HTTP/1.1 200 OK Accept-Ranges: bytes CF-Cache-Status: HIT CF-RAY: 4c4a68194daa0a66-LHR Cache-Control: public, max-age=7200 Connection: keep-alive Content-Length: 32066 Content-Type: image/png Date: Tue, 09 Apr 2019 06:25:56 GMT Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" Expires: Tue, 09 Apr 2019 08:25:56 GMT Last-Modified: Mon, 08 Apr 2019 04:54:25 GMT Server: cloudflare Set-Cookie: __cfduid=d3eea753081690f9a2d36495a74407dd71554791156; expires=Wed, 08-Apr-20 06:25:56 GMT; path=/; domain=.2daygeek.com; HttpOnly; Secure Vary: Accept-Encoding Downloading 31.31 kB to "Anbox-1.png" Done. 31.31 kB in 0.01551s (1.97 MB/s)
你可以使用带 -c
参数的 HTTPie 继续下载。
# http --download --continue https://speed.hetzner.de/100MB.bin -o 100MB.bin HTTP/1.1 206 Partial Content Connection: keep-alive Content-Length: 100442112 Content-Range: bytes 4415488-104857599/104857600 Content-Type: application/octet-stream Date: Tue, 09 Apr 2019 06:32:52 GMT ETag: "5253f0fd-6400000" Last-Modified: Tue, 08 Oct 2013 11:48:13 GMT Server: nginx Strict-Transport-Security: max-age=15768000; includeSubDomains Downloading 100.00 MB to "100MB.bin" | 24.14 % 24.14 MB 1.12 MB/s 0:01:07 ETA^C
你根据下面的输出验证是否同一个文件:
[email protected]:/var/log# ls -lhtr 100MB.bin -rw-r--r-- 1 root root 25M Apr 9 01:33 100MB.bin
你可以通过使用带有小于号 <
的 HTTPie 命令上传文件
$ http https://transfer.sh < Anbox-1.png
你可以使用带有重定向 >
符号的 HTTPie 命令下载文件。
# http https://www.2daygeek.com/wp-content/uploads/2019/03/How-To-Install-And-Enable-Flatpak-Support-On-Linux-1.png > Flatpak.png # ls -ltrh Flatpak.png -rw-r--r-- 1 root root 47K Apr 9 01:44 Flatpak.png
您可以在请求中发送 HTTP GET 方法。GET 方法会使用给定的 URI,从给定服务器检索信息。
# http GET httpie.org HTTP/1.1 301 Moved Permanently CF-RAY: 4c4a83a3f90dcbe6-SIN Cache-Control: max-age=3600 Connection: keep-alive Date: Tue, 09 Apr 2019 06:44:44 GMT Expires: Tue, 09 Apr 2019 07:44:44 GMT Location: https://httpie.org/ Server: cloudflare Transfer-Encoding: chunked Vary: Accept-Encoding
使用以下格式提交表单。POST 请求用于向服务器发送数据,例如客户信息、文件上传等。要使用 HTML 表单。
# http -f POST Ubuntu18.2daygeek.com hello='World' HTTP/1.1 200 OK Accept-Ranges: bytes Connection: Keep-Alive Content-Encoding: gzip Content-Length: 3138 Content-Type: text/html Date: Tue, 09 Apr 2019 06:48:12 GMT ETag: "2aa6-5844bf1b047fc-gzip" Keep-Alive: timeout=5, max=100 Last-Modified: Sun, 17 Mar 2019 15:29:55 GMT Server: Apache/2.4.29 (Ubuntu) Vary: Accept-Encoding
运行下面的指令以查看正在发送的请求。
# http -v Ubuntu18.2daygeek.com GET / HTTP/1.1 Accept: */* Accept-Encoding: gzip, deflate Connection: keep-alive Host: ubuntu18.2daygeek.com User-Agent: HTTPie/0.9.8 hello=World HTTP/1.1 200 OK Accept-Ranges: bytes Connection: Keep-Alive Content-Encoding: gzip Content-Length: 3138 Content-Type: text/html Date: Tue, 09 Apr 2019 06:48:30 GMT ETag: "2aa6-5844bf1b047fc-gzip" Keep-Alive: timeout=5, max=100 Last-Modified: Sun, 17 Mar 2019 15:29:55 GMT Server: Apache/2.4.29 (Ubuntu) Vary: Accept-Encoding
当前支持的身份验证认证方案是基本认证(Basic)和摘要验证(Digest)。
基本认证:
$ http -a username:password example.org
摘要验证:
$ http -A digest -a username:password example.org
提示输入密码:
$ http -a username example.org
via: https://www.2daygeek.com/httpie-curl-wget-alternative-http-client-linux/
作者:Magesh Maruthamuthu 选题:lujun9972 译者:zgj1024 校对:wxy