curlより便利なHTTPie のインストール方法と基本的な使い方

2020年11月28日

HTTPieとは?

HTTPieはcurlのようなCUIのHTTPクライアントツールです。JSONサポート、シンタックスハイライトなどWeb APIクライアントとして使いやすい機能が提供されています。

HTTPie公式ページ

インストール方法

MacユーザでHomebrewを使っている場合は以下のように簡単にインストールすることができます。

$ brew install httpie

Linuxの場合は、yum, aptでインストールすることができます。

# aptの場合
$ apt install httpie

# yumの場合
$ yum install httpie

Windowsの場合は、pipを使ってインストールすることができます。

# pipとsetuptoolsを最新化
$ pip install --upgrade pip setuptools

# HTTPieをインストール
$ pip install --upgrade httpie

基本的な使い方

GETリクエスト

GETリクエストを送信する場合、以下の例のように$http {URL} {パラメータ名}=={値} {HTTPヘッダ名}:{値} という形式になります。

# パラメータnameに"TestItem"、Authorizationヘッダに"key1"を指定してGETリクエストを投げる
$ http http://localhost:8080/items/1 name==TestItem Authorization:key1

HTTP/1.1 200
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Connection: keep-alive
Content-Type: application/json
Date: Thu, 23 Jul 2020 03:13:21 GMT
Expires: 0
Keep-Alive: timeout=60
Pragma: no-cache
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

{
    "id": 1,
    "name": "TestItem",
    "price": 100
}

-vオプションをつけるとリクエスト情報を表示することができます。また、リクエストヘッダを複数付けたい場合スペースで区切って指定します。

# -vオプションをつけてリクエスト情報を表示する
# リクエストヘッダにAuthorizationとContent-Typeを指定する。
$ http -v http://localhost:8080/items/1 Authorization:key1 X-Custom-Header:xxx
GET /items/1 HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Authorization: key1
Connection: keep-alive
X-Custom-Header: xxx
Host: localhost:8080
User-Agent: HTTPie/1.0.2


HTTP/1.1 200
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Connection: keep-alive
Content-Type: application/json
Date: Thu, 23 Jul 2020 03:18:57 GMT
Expires: 0
Keep-Alive: timeout=60
Pragma: no-cache
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

{
    "id": 1,
    "name": "TestItem",
    "price": 100
}

POSTリクエスト

APIに対してJSONをPOSTする例です。

http -v POST http://localhost:8080/items item:='{"xxx":100}'  Authorization:key1
POST /items HTTP/1.1
Accept: application/json, */*
Accept-Encoding: gzip, deflate
Authorization: key1
Connection: keep-alive
Content-Length: 22
Content-Type: application/json
Host: localhost:8080
User-Agent: HTTPie/1.0.2

{
    "item": {
        "xxx": 100
    }
}

HTTP/1.1 201
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Connection: keep-alive
Content-Type: application/json
Date: Thu, 23 Jul 2020 03:22:21 GMT
Expires: 0
Keep-Alive: timeout=60
Pragma: no-cache
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

{
    "id": 0,
    "name": null,
    "price": 0
}

localhostの省略

http://localhost:8080/items/1は:8080/items/1 のようにhttp://localhostを省略することができます。また、デフォルトのポートは80なのでhttp://localhost/items/1であれば/items/1のように書くことができます。

# localhostを省略する
$ http -v :8080/items/1 Authorization:key1

GET /items/1 HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Authorization: key1
Connection: keep-alive
Host: localhost:8080
User-Agent: HTTPie/1.0.2



HTTP/1.1 200
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Connection: keep-alive
Content-Type: application/json
Date: Thu, 23 Jul 2020 14:10:31 GMT
Expires: 0
Keep-Alive: timeout=60
Pragma: no-cache
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

{
    "id": 1,
    "name": "TestItem",
    "price": 100
}