本文介绍在Fedora、Debian/Ubuntu、Arch Linux、RHEL/CentOS、openSUSE系统中安装和使用HTTPie的方法,它是现代命令行http客户端,是curl和wget命令的最佳替代品。
HTTPie简介 HTTPie(发音为aitch-tee-tee-pie)是一个命令行HTTP客户端,它提供了一个简单的http命令,允许使用简单自然的语法发送任意HTTP请求,并显示彩色输出,HTTPie可用于测试,调试以及通常与HTTP服务器交互。 HTTPie主要特点如下: 直观的语法。 格式化和彩色化的终端输出。 内置JSON支持。 表单和文件上传。 HTTPS、代理和身份验证。 任意请求数据。 自定义标题。 似Wget的下载。 Python 2.7和3.x支持。
在Linux系统中安装HTTPie 1、对于Fedora系统,使用DNF安装httpie: $ sudo dnf install httpie 参考:dnf命令_Linux dnf命令使用详解:新一代的RPM软件包管理器。 2、对于Debian/Ubuntu系统,使用APT-GET或APT安装httpie: $ sudo apt install httpie 3、对于基于Arch Linux的系统,请使用Pacman安装httpie: $ sudo pacman -S httpie 4、对于RHEL/CentOS系统,请使用YUM安装httpie: $ sudo yum install httpie 5、对于openSUSE Leap系统,使用Zypper安装httpie: $ sudo zypper install httpie
使用HTTPie的方法 1)如何使用HTTPie请求URL? 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 2)如何使用HTTPie下载文件? 可以使用带有--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=dd2034b2f95ae42047e082f59; 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=d3eea753081690f9a2d36495a7; 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) 3)如何使用HTTPie恢复部分下载? 可以使用带有-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 可以在以下输出中验证相同内容: root@Ubuntu18:/var/log# ls -lhtr 100MB.bin -rw-r--r-- 1 root root 25M Apr 9 01:33 100MB.bin 5)如何使用HTTPie上传文件? 可以使用带有小于号码“<”符号的HTTPie上传文件: $ http https://transfer.sh < Anbox-1.png 6)如何使用带有重定向符号“>”的HTTPie下载文件? 可以使用带有重定向“>”符号的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 7)发送HTTP GET方法? 可以在请求中发送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 8)提交表格? 使用以下格式提交表单,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 9)HTTP身份验证? 当前支持的身份验证方案是Basic和Digest。 1、Basic auth: $ http -a username:password example.org 2、Digest auth: $ http -A digest -a username:password example.org 3、Password prompt: $ http -a username example.org 至此,HTTPie的基本使用方法讲完,掌握这些技巧,就能灵活的运用HTTPie了。
相关主题 |