Total Pageviews

Monday, 13 April 2015

HTTPie: a CLI, cURL-like tool for humans

HTTPie is a command line HTTP client. Its goal is to make CLI interaction with web services as human-friendly as possible. It provides a simple http command that allows for sending arbitrary HTTP requests using a simple and natural syntax, and displays colorized output. HTTPie can be used for testing, debugging, and generally interacting with HTTP servers.
HTTPie compared to cURL HTTPie is written in Python, and under the hood it uses the excellent Requests and Pygments libraries.

Latest version released on PyPi Build status of the master branch on Mac/Linux Build status of the master branch on Windows Test coverage


Main Features

  • Expressive and intuitive syntax
  • Formatted and colorized terminal output
  • Built-in JSON support
  • Forms and file uploads
  • HTTPS, proxies, and authentication
  • Arbitrary request data
  • Custom headers
  • Persistent sessions
  • Wget-like downloads
  • Python 2.6, 2.7 and 3.x support
  • Linux, Mac OS X and Windows support
  • Plugins
  • Documentation
  • Test coverage

Installation

On Mac OS X, HTTPie can be installed via Homebrew:
$ brew install httpie
Most Linux distributions provide a package that can be installed using the system package manager, e.g.:
# Debian-based distributions such as Ubuntu:
$ apt-get install httpie

# RPM-based distributions:
$ yum install httpie
A universal installation method (that works on Windows, Mac OS X, Linux, …, and provides the latest version) is to use pip:
# Make sure we have an up-to-date version of pip and setuptools:
$ pip install --upgrade pip setuptools

$ pip install --upgrade httpie
(If pip installation fails for some reason, you can try easy_install httpie as a fallback.)

Development version

The latest development version can be installed directly from GitHub:
# Mac OS X via Homebrew
$ brew install httpie --HEAD

# Universal
$ pip install --upgrade https://github.com/jakubroztocil/httpie/tarball/master

Usage

Hello World:
$ http httpie.org
Synopsis:
$ http [flags] [METHOD] URL [ITEM [ITEM]]
See also http --help.

FROM  https://github.com/jakubroztocil/httpie
-----------------------

HTTPie 采用 Python 开发,底层用到了 Requests 和 Pygments 库。

缺省支持json,比curl的语法更加简洁。

安装很容易:

pip install httpie
用法:

模拟提交表单
http -f POST example.org username=nate

显示详细的请求
http -v example.org

只显示Header
http -h example.org

只显示Body
http -b example.org

下载文件
http -d example.org

请求删除的方法
http DELETE example.org

传递JSON数据请求(默认就是JSON数据请求)
http PUT example.org ame=nate password=nate_password
如果JSON数据存在不是字符串则用:=分隔,例如
http PUT example.org name=nate password=nate_password age:=28 a:=true streets:='["a", "b"]'

模拟Form的Post请求, Content-Type: application/x-www-form-urlencoded; charset=utf-8
http --form POST example.org name='nate'
模拟Form的上传, Content-Type: multipart/form-data
http -f POST example.com/jobs name='John Smith' file@~/test.pdf

修改请求头, 使用:分隔
http example.org  User-Agent:Yhz/1.0  'Cookie:a=b;b=c'  Referer:http://example.org

认证
http -a username:password example.org
http --auth-type=digest -a username:password example.org

使用http代理
http --proxy=http:http://192.168.1.100:8060 example.org
http --proxy=http:http://user:pass@192.168.1.100:8060 example.org
进阶用法:

看全部并跟随跳转
http --all --follow httpbin.org/redirect/3

下载2进制文件
http example.org/Movie.mov > Movie.mov

下载图片,然后用imagemagick转化,然后上传到另外一个网站
 http octodex.github.com/images/original.jpg | convert - -resize 25% -  | http example.org/Octocats

把认证信息放到session里
http --session=user1 -a user1:password example.org X-Foo:Bar

使用之前认证过后的session继续访问
http --session=user1 example.org