もた日記

くだらないことを真面目にやる

補完、ハイライトが効くHTTP Prompt対話型クライアントを使ってみる

http-prompt


github.com
HTTP Prompt - An interactive command-line HTTP client(公式ページ)


HTTP Promptは自動補完、シンタックスハイライトが効くインタラクティブコマンドラインHTTPクライアント。
説明に書いてあるようにHTTPie (HTTPクライアント)+ prompt_toolkitインタラクティブコマンドラインライブラリ)のようなツール。

f:id:wonder-wall:20170724194914p:plain

prompt_toolkitを使ったツールには下記ツールもある。
wonderwall.hatenablog.com
wonderwall.hatenablog.com

インストー


pipでインストールできるので下記コードを実行。

$ pip install http-prompt
$ http-prompt --help
Usage: http-prompt [OPTIONS] [URL] [HTTP_OPTIONS]...

Options:
  --spec TEXT  OpenAPI/Swagger specification file.
  --version    Show the version and exit.
  --help       Show this message and exit.



使い方


コマンド操作例があったので試してみる。

操作例1

HTTP Promptasciinema.org

最初にHTTP Request & Responseサービスhttpbin.orgに対してセッションを開始する。

$ http-prompt httpbin.org
Version: 0.10.1

cdによりURLアドレスを変更する。

http://httpbin.org> cd post

Headerを設定する。

http://httpbin.org/post> Authorization:'Bearer accesstoken'
http://httpbin.org/post> Content-Type:application/json

Body Parameterを設定する。

http://httpbin.org/post> username=foo password=bar

httpieを先頭に付けてPOSTをプレビューする。

http://httpbin.org/post> httpie post
http POST http://httpbin.org/post password=bar username=foo 'Authorization:Bearer accesstoken' Content-Type:application/json

POSTする。

http://httpbin.org/post> post
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 545
Content-Type: application/json
Date: Mon, 24 Jul 2017 10:44:22 GMT
Server: meinheld/0.6.1
Via: 1.1 vegur
X-Powered-By: Flask
X-Processed-Time: 0.000778913497925

{
    "args": {},
    "data": "{\"password\": \"bar\", \"username\": \"foo\"}",
    "files": {},
    "form": {},
    "headers": {
        "Accept": "application/json, */*",
        "Accept-Encoding": "gzip, deflate",
        "Authorization": "Bearer accesstoken",
        "Connection": "close",
        "Content-Length": "38",
        "Content-Type": "application/json",
        "Host": "httpbin.org",
        "User-Agent": "HTTPie/0.9.6"
    },
    "json": {
        "password": "bar",
        "username": "foo"
    },
    "origin": "120.75.27.182",
    "url": "http://httpbin.org/post"
}

パイプを使って結果をgrepする(パイプも使える)。

http://httpbin.org/post> post | grep username
  "data": "{\"password\": \"bar\", \"username\": \"foo\"}",
    "username": "foo"

exitで終了する。

http://httpbin.org/post> exit
Goodbye!


操作例2

HTTP Prompt: ls and OpenAPI/Swagger specificationasciinema.org

Open APIの例。--specで指定する。

$ http-prompt https://api.github.com --spec=https://api.apis.guru/v2/specs/github.com/v3/swagger.json
Version: 0.10.1

lsも使える。

https://api.github.com> ls
emojis        feeds         gitignore     legacy        meta          notifications rate_limit    repositories  teams         users
events        gists         issues        markdown      networks      orgs          repos         search        user
https://api.github.com> cd repos
https://api.github.com/repos> ls
{owner}
https://api.github.com/repos> cd eliangcs/http-prompt/releases

GETする。

https://api.github.com/repos/eliangcs/http-prompt/releases> get
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval
Cache-Control: public, max-age=60, s-maxage=60
Content-Encoding: gzip
Content-Security-Policy: default-src 'none'
Content-Type: application/json; charset=utf-8
Date: Mon, 24 Jul 2017 11:02:36 GMT
ETag: W/"4b441f4ac7e9695642fbf634b6b8e22c"
Server: GitHub.com
Status: 200 OK
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
Transfer-Encoding: chunked
Vary: Accept
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
X-Frame-Options: deny
X-GitHub-Media-Type: github.v3; format=json
X-GitHub-Request-Id: E0C4:9A07:3E52A0D:60012A4:5975D3CB
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 55
X-RateLimit-Reset: 1500894968
X-Runtime-rack: 0.040319
X-XSS-Protection: 1; mode=block

[
    {
        "assets": [],
        "assets_url": "https://api.github.com/repos/eliangcs/http-prompt/releases/6353020/assets",
        "author": {
            "avatar_url": "https://avatars0.githubusercontent.com/u/740461?v=4",
            "events_url": "https://api.github.com/users/eliangcs/events{/privacy}",
            "followers_url": "https://api.github.com/users/eliangcs/followers",
            "following_url": "https://api.github.com/users/eliangcs/following{/other_user}",
            "gists_url": "https://api.github.com/users/eliangcs/gists{/gist_id}",
            "gravatar_id": "",
            "html_url": "https://github.com/eliangcs",
            "id": 740461,
            "login": "eliangcs",
            "organizations_url": "https://api.github.com/users/eliangcs/orgs",
            "received_events_url": "https://api.github.com/users/eliangcs/received_events",
            "repos_url": "https://api.github.com/users/eliangcs/repos",
            "site_admin": false,
            "starred_url": "https://api.github.com/users/eliangcs/starred{/owner}{/repo}",
            "subscriptions_url": "https://api.github.com/users/eliangcs/subscriptions",
            "type": "User",
            "url": "https://api.github.com/users/eliangcs"
        },
        "body": "* cd without an argument resets URL to root (#114)\r\n* Fix --spec crash issue on Windows (#116)",
        "created_at": "2017-05-11T13:22:53Z",
        "draft": false,
        "html_url": "https://github.com/eliangcs/http-prompt/releases/tag/v0.10.1",
        "id": 6353020,
        "name": "v0.10.1",
        "prerelease": false,
        "published_at": "2017-05-11T13:28:25Z",
        "tag_name": "v0.10.1",
        "tarball_url": "https://api.github.com/repos/eliangcs/http-prompt/tarball/v0.10.1",
        "target_commitish": "master",
        "upload_url": "https://uploads.github.com/repos/eliangcs/http-prompt/releases/6353020/assets{?name,label}",
        "url": "https://api.github.com/repos/eliangcs/http-prompt/releases/6353020",
        "zipball_url": "https://api.github.com/repos/eliangcs/http-prompt/zipball/v0.10.1"
    },
    {
        "assets": [],
        "assets_url": "https://api.github.com/repos/eliangcs/http-prompt/releases/6092744/assets",
        "author": {
            "avatar_url": "https://avatars0.githubusercontent.com/u/740461?v=4",

結果をjqコマンドで抽出する。--color-outputを指定すれば出力に色がつく(この画面ではわからないが)。

https://api.github.com/repos/eliangcs/http-prompt/releases> get | jq '.[] | {name, published_at}'
{
  "name": "v0.10.1",
  "published_at": "2017-05-11T13:28:25Z"
}
{
  "name": "v0.10.0",
  "published_at": "2017-04-16T15:48:04Z"
}
 …省略…
{
  "name": "v0.5.0",
  "published_at": "2016-09-16T02:06:53Z"
}
https://api.github.com/repos/eliangcs/http-prompt/releases> get | jq '.[] | {name, published_at}' --color-output
{
  "name": "v0.10.1",
  "published_at": "2017-05-11T13:28:25Z"
}
{
  "name": "v0.10.0",
  "published_at": "2017-04-16T15:48:04Z"
}
 …省略…
{
  "name": "v0.5.0",
  "published_at": "2016-09-16T02:06:53Z"
}
https://api.github.com/repos/eliangcs/http-prompt/releases> exit
Goodbye!


その他の操作例

リダイレクトを使って保存することも可能。

# Write output to a file
> COMMAND > /path/to/file

# Append output to a file
> COMMAND >> /path/to/file

envでカレントセッションの情報を確認可能。

> env
--verify=no
cd http://localhost
page==10
limit==20

Header情報などは削除可能。

# Remove a header
> rm -h Content-Type

# Remove a querystring parameter
> rm -q apikey

# Remove a body parameter
> rm -b username

# Remove an HTTPie option
> rm -o --auth

# To reset the session, i.e., clear all parameters and options:
> rm *

セッションの保存と復元も可能。

> env > /path/to/file
# Update the current session
> source /path/to/file

# Wipe out the current session and load from a file
> exec /path/to/file