1
0
mirror of https://github.com/httpie/cli.git synced 2024-11-24 08:22:22 +02:00
httpie-cli/README.rst

442 lines
19 KiB
ReStructuredText
Raw Normal View History

2012-07-26 10:01:30 +03:00
=======================
2012-03-05 02:48:06 +03:00
HTTPie: cURL for humans
=======================
2012-02-25 15:39:38 +03:00
`README for stable version`_
2012-07-26 11:03:34 +03:00
2012-07-17 08:01:30 +03:00
**HTTPie is a CLI HTTP utility** built out of frustration with existing tools.
2012-07-26 10:01:30 +03:00
Its goal is to make CLI interaction with HTTP-based services as
**human-friendly** as possible. HTTPie provides an ``http`` command that allows
for issuing **arbitrary HTTP** requests using a **simple and natural syntax**,
and displays **colorized responses**:
2012-03-04 13:54:27 +03:00
2012-03-05 02:48:06 +03:00
.. image:: https://github.com/jkbr/httpie/raw/master/httpie.png
:alt: HTTPie compared to cURL
2012-02-25 15:39:38 +03:00
2012-07-26 10:01:30 +03:00
HTTPie supports Python 2.6+ (including Python 3.x and PyPy) and has been tested
under Mac OS X, Linux and Windows. It also has a
comprehensive `suite of tests`_ with `continuous integration`_.
2012-02-25 16:42:06 +03:00
2012-07-26 10:01:30 +03:00
Under the hood, the excellent `Requests`_ and `Pygments`_ Python libraries
are used.
2012-02-25 15:39:38 +03:00
2012-07-26 10:01:30 +03:00
Installation
============
2012-07-26 10:01:30 +03:00
The latest **stable version** of HTTPie can always be installed or updated
to via `pip`_ (prefered)
or ``easy_install``:
.. code-block:: shell
2012-07-20 23:09:53 +03:00
2012-07-26 10:01:30 +03:00
pip install -U httpie
# easy_install httpie
2012-03-05 02:48:06 +03:00
2012-03-05 02:48:06 +03:00
Or, you can install the **development version** directly from GitHub:
.. image:: https://secure.travis-ci.org/jkbr/httpie.png
:target: http://travis-ci.org/jkbr/httpie
:alt: Build Status of the master branch
.. code-block:: shell
pip install -U https://github.com/jkbr/httpie/tarball/master
2012-02-25 15:39:38 +03:00
2012-07-26 10:01:30 +03:00
There are also packages available for `Ubuntu`_, `Debian`_ and possibly other
distributions as well.
2012-03-05 02:48:06 +03:00
Usage
2012-07-26 10:01:30 +03:00
=====
2012-03-05 02:48:06 +03:00
Hello world::
2012-06-17 20:46:56 +03:00
http httpie.org
2012-03-05 02:48:06 +03:00
Synopsis::
2012-06-17 20:46:56 +03:00
http [flags] [METHOD] URL [items]
2012-02-25 15:39:38 +03:00
2012-07-26 10:01:30 +03:00
There are five different types of key/value pair ``items`` available:
+-----------------------+-----------------------------------------------------+
| **Headers** | Arbitrary HTTP headers. The ``:`` character is |
| ``Name:Value`` | used to separate a header's name from its value, |
| | e.g., ``X-API-Token:123``. |
+-----------------------+-----------------------------------------------------+
| **Simple data | Included in the request body and depending on the |
| fields** | ``Content-Type`` they are automatically serialized |
| ``field=value`` | as a JSON ``Object`` (default) or |
| | ``application/x-www-form-urlencoded`` |
| | (``--form``/ ``-f``). Data items use ``=`` |
| | as the separator, e.g., ``hello=world``. |
+-----------------------+-----------------------------------------------------+
| **Raw JSON fields** | Useful when the ``Content-Type`` is JSON and one or |
| ``field:=json`` | more fields need to be a ``Boolean``, ``Number``, |
| | nested ``Object``, or an ``Array``. It's because |
| | simple data items are always serialized as a |
| | ``String``. E.g., ``pies:=[1,2,3]``, or |
| | ``'meals:=["ham","spam"]'`` (note the quotes). |
| | It may be more convenient to pass the whole JSON |
| | body via ``stdin`` when it's more complex |
| | (see examples bellow). |
+-----------------------+-----------------------------------------------------+
| **File fields** | Only available with ``-f`` / ``--form``. Use ``@`` |
| ``field@/dir/file`` | as the separator, e.g., |
| | ``screenshot@~/Pictures/img.png``. |
| | The presence of a file field results |
| | into a ``multipart/form-data`` request. |
+-----------------------+-----------------------------------------------------+
| **Query string | Appends the given name/value pair as a query |
| parameters** | string parameter to the URL. |
| ``name==value`` | The ``==`` separator is used |
+-----------------------+-----------------------------------------------------+
All ``items`` come after the URL, and, unlike ``flags``, they become part of
the actual request being is sent. Their types are distinguished by the
separator used.
2012-03-15 00:55:09 +03:00
2012-03-05 02:48:06 +03:00
Examples
2012-07-26 10:01:30 +03:00
--------
.. code-block:: shell
2012-03-05 02:48:06 +03:00
http PATCH api.example.com/person/1 X-API-Token:123 name=John email=john@example.org age:=29
The following request is issued:
.. code-block:: javascript
2012-02-25 15:47:23 +03:00
PATCH /person/1 HTTP/1.1
User-Agent: HTTPie/0.1
X-API-Token: 123
Content-Type: application/json; charset=utf-8
2012-03-05 02:48:06 +03:00
{"name": "John", "email": "john@example.org", "age": 29}
2012-02-27 13:54:41 +03:00
2012-07-17 08:01:30 +03:00
It can easily be changed to a **form** request using the ``-f``
(or ``--form``) flag, which produces::
2012-03-05 02:48:06 +03:00
PATCH /person/1 HTTP/1.1
User-Agent: HTTPie/0.1
X-API-Token: 123
Content-Type: application/x-www-form-urlencoded; charset=utf-8
2012-03-05 02:48:06 +03:00
age=29&name=John&email=john%40example.org
2012-07-17 08:01:30 +03:00
It is also possible to send ``multipart/form-data`` requests, i.e., to
simulate a **file upload form** submission. It is done using the
``--form`` / ``-f`` flag and passing one or more file fields:
.. code-block:: shell
2012-03-15 00:55:09 +03:00
http -f POST example.com/jobs name=John cv@~/Documents/cv.pdf
2012-07-26 10:01:30 +03:00
The above will send the same request as if the following HTML form were
submitted:
.. code-block:: html
2012-03-15 00:55:09 +03:00
<form enctype="multipart/form-data" method="post" action="http://example.com/jobs">
<input type="text" name="name" />
<input type="file" name="cv" />
</form>
2012-07-26 10:01:30 +03:00
**Query string parameters** can be added to any request without having to
escape the ``&`` characters. The following request will contain
``?search=donuts&in=fridge`` as the query string part of the URL:
.. code-block:: shell
2012-07-26 10:01:30 +03:00
http GET example.com search==donuts in==fridge
2012-07-26 10:01:30 +03:00
The whole request body can also be passed in **via stdin,** in which
case it will be used with no further processing:
.. code-block:: shell
2012-02-25 15:39:38 +03:00
2012-03-05 02:48:06 +03:00
echo '{"name": "John"}' | http PATCH example.com/person/1 X-API-Token:123
# Or:
http POST example.com/person/1 X-API-Token:123 < person.json
2012-02-25 15:47:23 +03:00
2012-07-17 08:01:30 +03:00
That can be used for **piping services together**. The following example
``GET``-s JSON data from the Github API and ``POST``-s it to httpbin.org:
.. code-block:: shell
2012-06-25 15:50:49 +03:00
http GET https://api.github.com/repos/jkbr/httpie | http POST httpbin.org/post
2012-06-25 15:50:49 +03:00
2012-07-17 08:01:30 +03:00
The above can be further simplified by omitting ``GET`` and ``POST`` because
2012-07-26 10:01:30 +03:00
they are both default here as the first command has no request data whereas
the second one has via ``stdin``:
.. code-block:: shell
2012-06-25 15:50:49 +03:00
http https://api.github.com/repos/jkbr/httpie | http httpbin.org/post
2012-06-25 15:50:49 +03:00
Note that when the **output is redirected** (like the examples above), HTTPie
2012-07-26 10:01:30 +03:00
applies a different set of defaults than for a console output. Namely, colors
aren't used (unless ``--pretty`` is set) and only the response body
is printed (unless ``--print`` options specified). It is a convenience
that allows for things like the one above or downloading (smallish) binary
files without having to set any flags:
.. code-block:: shell
http www.google.com/favicon.ico > favicon.ico
2012-07-26 10:01:30 +03:00
An alternative to ``stdin`` is to pass a filename whose content will be used
2012-07-17 08:01:30 +03:00
as the request body. It has the advantage that the ``Content-Type`` header
will automatically be set to the appropriate value based on the filename
2012-07-26 10:01:30 +03:00
extension. Thus, the following will request will send the verbatim contents
of the file with ``Content-Type: application/xml``:
.. code-block:: shell
http PUT httpbin.org/put @/data/file.xml
2012-07-26 10:01:30 +03:00
When using HTTPie from **shell scripts** it can be useful to use the
``--check-status`` flag. It instructs HTTPie to exit with an error if the
HTTP status is one of ``3xx``, ``4xx``, or ``5xx``. The exit status will
2012-07-26 10:01:30 +03:00
be ``3`` (unless ``--allow-redirects`` is set), ``4``, or ``5``,
respectively:
.. code-block:: shell
#!/bin/bash
2012-07-24 02:17:07 +03:00
if http --check-status HEAD example.org/health &> /dev/null; then
echo 'OK!'
else
case $? in
3) echo 'Unexpected 3xx Redirection!' ;;
4) echo '4xx Client Error!' ;;
5) echo '5xx Server Error!' ;;
*) echo 'Other Error!' ;;
esac
fi
2012-02-27 13:54:41 +03:00
2012-03-05 02:48:06 +03:00
Flags
2012-07-26 10:01:30 +03:00
-----
2012-07-26 10:01:30 +03:00
``$ http --help``::
2012-07-26 01:26:23 +03:00
usage: http [--help] [--version] [--json | --form] [--traceback]
2012-06-24 04:43:08 +03:00
[--pretty | --ugly]
[--print OUTPUT_OPTIONS | --verbose | --headers | --body]
[--style STYLE] [--check-status] [--auth AUTH]
[--auth-type {basic,digest}] [--verify VERIFY] [--proxy PROXY]
[--allow-redirects] [--timeout TIMEOUT]
2012-06-24 04:43:08 +03:00
[METHOD] URL [ITEM [ITEM ...]]
2012-04-25 03:10:58 +03:00
HTTPie - cURL for humans. <http://httpie.org>
2012-03-04 04:48:31 +03:00
positional arguments:
2012-04-25 03:10:58 +03:00
METHOD The HTTP method to be used for the request (GET, POST,
2012-06-24 04:43:08 +03:00
PUT, DELETE, PATCH, ...). If this argument is omitted,
then HTTPie will guess the HTTP method. If there is
some data to be sent, then it will be POST, otherwise
GET.
2012-04-25 03:10:58 +03:00
URL The protocol defaults to http:// if the URL does not
include one.
ITEM A key-value pair whose type is defined by the
separator used. It can be an HTTP header
2012-07-21 00:51:05 +03:00
(header:value), a data field to be used in the request
body (field_name=value), a raw JSON data field
(field_name:=value), a query parameter (name==value),
2012-07-21 00:51:05 +03:00
or a file field (field_name@/path/to/file). You can
use a backslash to escape a colliding separator in the
field name.
2012-03-04 04:48:31 +03:00
optional arguments:
--help show this help message and exit
2012-03-04 13:22:09 +03:00
--version show program's version number and exit
--json, -j (default) Data items from the command line are
serialized as a JSON object. The Content-Type and
Accept headers are set to application/json (if not
specified).
--form, -f Data items from the command line are serialized as
form fields. The Content-Type is set to application/x
-www-form-urlencoded (if not specified). The presence
of any file fields results into a multipart/form-data
request.
2012-03-04 13:22:09 +03:00
--traceback Print exception traceback should one occur.
--pretty If stdout is a terminal, the response is prettified by
2012-03-04 13:22:09 +03:00
default (colorized and indented if it is JSON). This
flag ensures prettifying even when stdout is
redirected.
2012-03-04 04:48:31 +03:00
--ugly, -u Do not prettify the response.
--print OUTPUT_OPTIONS, -p OUTPUT_OPTIONS
2012-07-21 00:51:05 +03:00
String specifying what the output should contain: "H"
stands for the request headers, and "B" for the
request body. "h" stands for the response headers and
"b" for response the body. The default behaviour is
"hb" (i.e., the response headers and body is printed),
if standard output is not redirected. If the output is
piped to another program or to a file, then only the
body is printed by default.
2012-04-25 03:10:58 +03:00
--verbose, -v Print the whole request as well as the response.
Shortcut for --print=HBhb.
--headers, -h Print only the response headers. Shortcut for
--print=h.
--body, -b Print only the response body. Shortcut for --print=b.
2012-03-04 04:48:31 +03:00
--style STYLE, -s STYLE
Output coloring style, one of autumn, borland, bw,
colorful, default, emacs, friendly, fruity, manni,
2012-06-24 04:43:08 +03:00
monokai, murphy, native, pastie, perldoc, rrt,
solarized, tango, trac, vim, vs. Defaults to
solarized. For this option to work properly, please
make sure that the $TERM environment variable is set
to "xterm-256color" or similar (e.g., via `export TERM
=xterm-256color' in your ~/.bashrc).
--check-status By default, HTTPie exits with 0 when no network or
other fatal errors occur. This flag instructs HTTPie
to also check the HTTP status code and exit with an
error if the status indicates one. When the server
replies with a 4xx (Client Error) or 5xx (Server
Error) status code, HTTPie exits with 4 or 5
respectively. If the response is a 3xx (Redirect) and
--allow-redirects hasn't been set, then the exit
status is 3. Also an error message is written to
stderr if stdout is redirected.
2012-07-21 00:51:05 +03:00
--auth AUTH, -a AUTH username:password. If only the username is provided
(-a username), HTTPie will prompt for the password.
2012-04-25 03:10:58 +03:00
--auth-type {basic,digest}
The authentication mechanism to be used. Defaults to
"basic".
--verify VERIFY Set to "no" to skip checking the host's SSL
certificate. You can also pass the path to a CA_BUNDLE
file for private certs. You can also set the
REQUESTS_CA_BUNDLE environment variable. Defaults to
"yes".
2012-03-04 04:48:31 +03:00
--proxy PROXY String mapping protocol to the URL of the proxy (e.g.
http:foo.bar:3128).
--allow-redirects Set this flag if full redirects are allowed (e.g. re-
POST-ing of data at new ``Location``)
--timeout TIMEOUT Float describes the timeout of the request (Use
socket.setdefaulttimeout() as fallback).
2012-03-04 15:33:18 +03:00
2012-07-26 10:01:30 +03:00
2012-06-15 18:13:40 +03:00
Contribute
2012-07-26 10:01:30 +03:00
==========
2012-07-26 10:01:30 +03:00
Bug reports and code and documentation patches are greatly appretiated. You can
also help by using the development version of HTTPie and reporting any bugs you
might encounter.
2012-07-26 10:01:30 +03:00
Before working on a new feature or a bug, please browse the `existing issues`_
to see whether it has been previously discussed.
2012-06-15 18:13:40 +03:00
2012-07-26 10:01:30 +03:00
Then fork and clone `the repository`_.
2012-06-15 18:13:40 +03:00
2012-07-26 10:01:30 +03:00
To point the ``http`` command to your local branch during development you can
install HTTPie in an editable mode::
pip install --editable .
2012-07-26 10:01:30 +03:00
To run the existing suite of tests before a pull request is submitted::
2012-06-15 18:13:40 +03:00
python setup.py test
2012-07-26 10:01:30 +03:00
`Tox`_ can also be used to conveniently run tests in all of the
`supported Python environments`_::
2012-06-15 18:13:40 +03:00
2012-06-15 18:32:45 +03:00
# Install tox
2012-06-15 18:13:40 +03:00
pip install tox
2012-06-15 18:32:45 +03:00
# Run tests
tox
2012-07-30 13:10:19 +03:00
Don't forget to add yourself to `AUTHORS`_.
2012-07-26 10:01:30 +03:00
Authors
=======
`Jakub Roztocil`_ (`@jakubroztocil`_) created HTTPie and `these fine people`_
have contributed.
2012-03-05 02:48:06 +03:00
Changelog
2012-07-26 10:01:30 +03:00
=========
2012-03-04 15:33:18 +03:00
2012-07-26 10:01:30 +03:00
* `0.2.7dev`_
* Windows: Added ``--output FILE`` to store output into a file
(piping results into corrupted data on Windows).
* Proper handling of binary requests and responses.
* Fixed printing of ``multipart/form-data`` requests.
* Renamed ``--traceback`` to ``--debug``.
2012-07-26 10:01:30 +03:00
* `0.2.6`_ (2012-07-26)
* The short option for ``--headers`` is now ``-h`` (``-t`` has been
removed, for usage use ``--help``).
* Form data and URL parameters can have multiple fields with the same name
2012-07-24 18:22:04 +03:00
(e.g.,``http -f url a=1 a=2``).
2012-07-26 10:01:30 +03:00
* Added ``--check-status`` to exit with an error on HTTP 3xx, 4xx and
5xx (3, 4, and 5, respectively).
* If the output is piped to another program or redirected to a file,
2012-07-26 10:01:30 +03:00
the default behaviour is to only print the response body.
2012-07-26 01:26:23 +03:00
(It can still be overwritten via the ``--print`` flag.)
* Improved highlighting of HTTP headers.
2012-07-26 10:01:30 +03:00
* Added query string parameters (``param==value``).
2012-07-17 05:20:37 +03:00
* Added support for terminal colors under Windows.
2012-07-26 10:01:30 +03:00
* `0.2.5`_ (2012-07-17)
2012-07-17 08:01:30 +03:00
* Unicode characters in prettified JSON now don't get escaped for
improved readability.
2012-07-17 01:05:09 +03:00
* --auth now prompts for a password if only a username provided.
2012-07-17 08:01:30 +03:00
* Added support for request payloads from a file path with automatic
``Content-Type`` (``http URL @/path``).
2012-07-26 01:26:23 +03:00
* Fixed missing query string when displaying the request headers via
2012-07-17 08:01:30 +03:00
``--verbose``.
* Fixed Content-Type for requests with no data.
2012-07-26 10:01:30 +03:00
* `0.2.2`_ (2012-06-24)
2012-07-17 08:01:30 +03:00
* The ``METHOD`` positional argument can now be omitted (defaults to
``GET``, or to ``POST`` with data).
2012-06-24 04:43:08 +03:00
* Fixed --verbose --form.
2012-07-26 10:01:30 +03:00
* Added support for `Tox`_.
* `0.2.1`_ (2012-06-13)
2012-06-13 17:01:23 +03:00
* Added compatibility with ``requests-0.12.1``.
2012-07-17 08:01:30 +03:00
* Dropped custom JSON and HTTP lexers in favor of the ones newly included
in ``pygments-1.5``.
2012-07-26 10:01:30 +03:00
* `0.2.0`_ (2012-04-25)
2012-04-25 03:10:58 +03:00
* Added Python 3 support.
2012-07-17 08:01:30 +03:00
* Added the ability to print the HTTP request as well as the response
(see ``--print`` and ``--verbose``).
2012-04-25 03:10:58 +03:00
* Added support for Digest authentication.
2012-07-17 08:01:30 +03:00
* Added file upload support
(``http -f POST file_field_name@/path/to/file``).
2012-04-25 03:10:58 +03:00
* Improved syntax highlighting for JSON.
* Added support for field name escaping.
* Many bug fixes.
2012-07-26 10:01:30 +03:00
* `0.1.6`_ (2012-03-04)
.. _suite of tests: https://github.com/jkbr/httpie/blob/master/tests/tests.py
.. _continuous integration: http://travis-ci.org/#!/jkbr/httpie
.. _Requests: http://python-requests.org
.. _Pygments: http://pygments.org/
.. _pip: http://www.pip-installer.org/en/latest/index.html
.. _Tox: http://tox.testrun.org
.. _supported Python environments: https://github.com/jkbr/httpie/blob/master/tox.ini
.. _Ubuntu: http://packages.ubuntu.com/httpie
.. _Debian: http://packages.debian.org/httpie
.. _the repository: https://github.com/jkbr/httpie
.. _these fine people: https://github.com/jkbr/httpie/contributors
2012-07-26 10:01:30 +03:00
.. _Jakub Roztocil: http://roztocil.name
.. _@jakubroztocil: https://twitter.com/jakubroztocil
2012-07-26 10:01:30 +03:00
.. _existing issues: https://github.com/jkbr/httpie/issues?state=open
.. _0.1.6: https://github.com/jkbr/httpie/compare/0.1.4...0.1.6
.. _0.2.0: https://github.com/jkbr/httpie/compare/0.1.6...0.2.0
.. _0.2.1: https://github.com/jkbr/httpie/compare/0.2.0...0.2.1
.. _0.2.2: https://github.com/jkbr/httpie/compare/0.2.1...0.2.2
.. _0.2.5: https://github.com/jkbr/httpie/compare/0.2.2...0.2.5
.. _0.2.6: https://github.com/jkbr/httpie/compare/0.2.5...0.2.6
.. _0.2.7dev: https://github.com/jkbr/httpie/compare/0.2.6...master
.. _README for stable version: https://github.com/jkbr/httpie/tree/0.2.6#readme
2012-07-30 13:10:19 +03:00
.. _AUTHORS: https://github.com/jkbr/httpie/blob/master/AUTHORS.rst