LoginSignup
2
3

More than 5 years have passed since last update.

windowsのgit bashからhttpieを使う

Last updated at Posted at 2016-12-14

やりたいこと

タイトル通りですが、httpieが便利だったので、windowsのgit-bashから使えないかなーと思い試してみました。

curlを捨ててhttpieを使おう - Qiita
http://qiita.com/yuku_t/items/5df06d50c4c349cc0c1b

手順

まず普通にwindowsのpython(ここでは2.7を入れた)をインストール

Python Releases for Windows | Python.org
https://www.python.org/downloads/windows/
Download Windows x86-64 MSI installer

そのあと、Pathを通す

pip と ipython インストール手引き (Windows編) - secretbase.log
http://cointoss.hatenablog.com/entry/20120531/1338474875

再起動

すると、git-bashから

[12-14 18:22:36 ~]$ python --version
Python 2.7.13rc1
[12-14 18:22:37 ~]$ pip --version
pip 9.0.1 from c:\python27\lib\site-packages (python 2.7)
[12-14 18:22:41 ~]$ easy_install --version
setuptools 31.0.0 from c:\python27\lib\site-packages (Python 2.7)
[12-14 18:22:46 ~]$

が使えるようになる。

次に、httpieをインストール

[12-14 18:17:08 ~]$ winpty pip install httpie
Collecting httpie
  Using cached httpie-0.9.9-py2.py3-none-any.whl
Requirement already satisfied: colorama>=0.2.4; sys_platform ==
"win32" in c:\python27\lib\site-packages (from httpie)
Requirement already satisfied: requests>=2.11.0 in
c:\python27\lib\site-packages (from httpie)
Requirement already satisfied: Pygments>=2.1.3 in
c:\python27\lib\site-packages (from httpie)
Installing collected packages: httpie
Successfully installed httpie-0.9.9
[12-14 18:19:24 ~]$

実行

[12-14 18:19:25 ~]$ winpty http https://www.google.co.jp/
HTTP/1.1 200 OK
Alt-Svc: quic=":443"; ma=2592000; v="35,34"
Cache-Control: private, max-age=0
Content-Encoding: gzip
Content-Type: text/html; charset=Shift_JIS
Date: Wed, 14 Dec 2016 09:19:30 GMT
...

便利!

...ところで、winptyってなによ!?

Git for WindowsでPythonがうまく動かない場合の対処法 - Qiita
http://qiita.com/icoxfog417/items/33e6b1f3dd9358923e21

ってことらしい。スクリプトをいじるか、
大人しくwinptyを頭につけて実行しよう。

http: error: Only terminal output can be colorized on Windows.ってでる

git-bashからhttpieの実行結果をtextに書き込むと、

$ winpty http GET http://127.0.0.1/search/song word==ai count==5 --print hb --pretty format --output result.txt

みたいなことをしたい場合、

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] [--default-scheme DEFAULT_SCHEME] [--debug]
            [METHOD] URL [REQUEST_ITEM [REQUEST_ITEM ...]]
http: error: Only terminal output can be colorized on Windows.

になってしまいます。この問題を解決するには、git-bashを使わず、cmd.exeで

C:\Users\XXXX> http GET http://127.0.0.1/search/song word==ai count==5 --print hb --pretty format --output result.txt

ってすればフォーマットされた状態のアウトプットを書き込めます。winpty要らない点に注意。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3