Help us understand the problem. What is going on with this article?

windowsのgit bashからhttpieを使う

More than 3 years have passed since last update.

やりたいこと

タイトル通りですが、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要らない点に注意。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした