httpie – Linux 上的 Curl 替代 HTTP 命令行客戶端

在本教程中,我將向您展示 httpie 這是一個現代命令行 HTTP 客戶端的替代工具 curl. httpie 是一個 GitHub 項目 雅庫布·羅茲托西爾 這使 Github 上的一些貢獻受益。 httpie 工具被視為現代命令行 http 客戶端,它使 CLI 與 Web 服務進行交互,並提供允許使用簡單自然的語法發送任意 HTTP 請求的 http 命令,並顯示彩色輸出。

它支持一些特性,如內置 JSON、HTTPS、語法著色、代理、身份驗證、持久會話、對 Python(2.6、2.7 和 3.x)的良好支持以及其他一些你可以發現的特性。

默認情況下,httpie 存在於您的 Linux 存儲庫中,可以通過軟件包直接安裝

# yum install httpie
Loaded plugins: fastestmirror
base                                                                                                                           | 3.6 kB  00:00:00     
epel/x86_64/metalink                                                                                                           |  13 kB  00:00:00     
epel                                                                                                                           | 4.3 kB  00:00:00     
Dependencies Resolved

======================================================================================================================================================
 Package                               Arch                         Version                                          Repository                  Size
======================================================================================================================================================
Installing:
 python2-httpie                        noarch                       0.9.4-1.el7                                      epel                       100 k
Installing for dependencies:
 jbigkit-libs                          x86_64                       2.0-11.el7                                       base                        46 k
 libtiff                               x86_64                       4.0.3-27.el7_3                                   base                       170 k
 libwebp                               x86_64                       0.3.0-7.el7                                      base                       170 k
 python-chardet                        noarch                       2.2.1-1.el7_1                                    base                       227 k

Installed:
  python2-httpie.noarch 0:0.9.4-1.el7                                                                                                                 

Dependency Installed:
  jbigkit-libs.x86_64 0:2.0-11.el7               libtiff.x86_64 0:4.0.3-27.el7_3                        libwebp.x86_64 0:0.3.0-7.el7                
  python-chardet.noarch 0:2.2.1-1.el7_1          python-pillow.x86_64 0:2.0.0-19.gitd1c6db8.el7         python-pygments.noarch 0:1.4-10.el7         
  python-requests.noarch 0:2.6.0-1.el7_1         python-six.noarch 0:1.9.0-2.el7                        python-urllib3.noarch 0:1.10.2-3.el7        

Complete!

2)如何使用httpie

現在我們已經安裝了 httpie,我們必須保留以下語法。

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

我們可以通過 help 命令查看如何使用它

# http --help
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] [--debug]
            [METHOD] URL [REQUEST_ITEM [REQUEST_ITEM ...]]

HTTPie - a CLI, cURL-like tool for humans. <https://httpie.org>

METHOD
      The HTTP method to be used for the request (GET, POST, PUT, DELETE, ...).
      
      This argument can be omitted in which case HTTPie will use POST if there
      is some data to be sent, otherwise GET:
      
          $ http example.org               # => GET
          $ http example.org hello=world   # => POST

必需的參數是 URL,以便使用該命令。 為了 example,您可以使用 httpie 通過終端直接在網站上發表評論,如下所示:

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

可以直接到github issue查看結果 帖子頁面 項目的

a) 請求網址

httpie 的基本用法是請求網站 URL。

# http google.com

b) 下載文件

你可以下載一個文件 --download htppie 的參數。 它作為 wget 命令工作

# http --download https://blog.linoxide.com/image/2017/09/kubernetes-monitoring.jpg

在此過程中可以直接重命名下載的文件-o 參數如下:

# http --download https://blog.linoxide.com/image/2017/09/kubernetes-monitoring.jpg -o kubernetes.jpg

當您使用 -o 參數,您可以使用 -c 範圍

c) 對 URL 進行身份驗證

httpie 支持通過兩種方法進行身份驗證: 基本的 消化 驗證。 您可以在此過程中直接在終端上插入密碼,但您也可以選擇在此過程中提示您輸入密碼。 對於提示密碼的基本認證,請執行以下操作:

# http -a alainfrancois linoxide.com
https: password for [email protected]:

或者您可以直接使用您的密碼,如下所示:

# http -a alainfrancois:password linoxide.com

一些額外的身份驗證機制可以作為插件安裝,例如 AWS / Amazon S3 (