mirror of
https://github.com/httpie/cli.git
synced 2024-11-24 08:22:22 +02:00
parent
73d0f9cd56
commit
b802f2b960
18
README.md
18
README.md
@ -7,7 +7,14 @@ HTTPie is a CLI frontend for [python-requests](http://python-requests.org) built
|
||||
|
||||
### Installation
|
||||
|
||||
pip install httpie
|
||||
Latest stable version using [pip](http://www.pip-installer.org/en/latest/index.html):
|
||||
|
||||
pip install -U httpie
|
||||
# easy_install httpie
|
||||
|
||||
Master:
|
||||
|
||||
pip install -U https://github.com/jkbr/httpie/tarball/master
|
||||
|
||||
|
||||
### Usage
|
||||
@ -27,6 +34,14 @@ Will issue the following request:
|
||||
|
||||
{"name": "John", "email": "john@example.org"}
|
||||
|
||||
You can pass other types then just strings using the `field:=value` notation. It allows you to set arbitrary JSON to the data fields:
|
||||
|
||||
http PUT httpie.org/pies bool:=true list:=[1,2,3] 'object:={"a": "b", "c": "d"}'
|
||||
|
||||
Produces the following JSON request:
|
||||
|
||||
{"bool": true, "list": [1, 2, 3], "object": {"a": "b", "c": "d"}}
|
||||
|
||||
You can use the `--form` flag to set `Content-Type` and serialize the data as `application/x-www-form-urlencoded`.
|
||||
|
||||
The data to be sent can also be passed via `stdin`:
|
||||
@ -34,6 +49,7 @@ The data to be sent can also be passed via `stdin`:
|
||||
http PUT api.example.com/person/1 X-API-Token:123 < person.json
|
||||
|
||||
Most of the flags mirror the arguments you would use with `requests.request`. See `http -h`:
|
||||
|
||||
usage: http [-h] [--json | --form] [--traceback] [--ugly] [--headers | --body]
|
||||
[--request] [--style STYLE] [--auth AUTH] [--verify VERIFY]
|
||||
[--proxy PROXY] [--allow-redirects] [--file PATH]
|
||||
|
27
httpie/httpie.py
Executable file → Normal file
27
httpie/httpie.py
Executable file → Normal file
@ -14,6 +14,7 @@ from . import __doc__ as doc
|
||||
DEFAULT_UA = 'HTTPie/%s' % version
|
||||
SEP_COMMON = ':'
|
||||
SEP_DATA = '='
|
||||
SEP_DATA_RAW = ':='
|
||||
TYPE_FORM = 'application/x-www-form-urlencoded; charset=utf-8'
|
||||
TYPE_JSON = 'application/json; charset=utf-8'
|
||||
PRETTIFY_STDOUT_TTY_ONLY = object()
|
||||
@ -33,6 +34,7 @@ class KeyValueType(object):
|
||||
if string.find(sep) != -1)
|
||||
|
||||
if not found:
|
||||
#noinspection PyExceptionInherit
|
||||
raise argparse.ArgumentTypeError(
|
||||
'"%s" is not a valid value' % string)
|
||||
sep = found[min(found.keys())]
|
||||
@ -109,8 +111,9 @@ parser.add_argument('url', metavar='URL',
|
||||
help='Protocol defaults to http:// if the'
|
||||
' URL does not include it.')
|
||||
parser.add_argument('items', nargs='*',
|
||||
type=KeyValueType([SEP_COMMON, SEP_DATA]),
|
||||
help='HTTP header (key:value) or data field (key=value)')
|
||||
type=KeyValueType([SEP_COMMON, SEP_DATA, SEP_DATA_RAW]),
|
||||
help='HTTP header (key:value), data field (key=value)'
|
||||
' or raw JSON field (field:=value).')
|
||||
|
||||
|
||||
def main(args=None,
|
||||
@ -127,14 +130,23 @@ def main(args=None,
|
||||
headers['User-Agent'] = DEFAULT_UA
|
||||
data = {}
|
||||
for item in args.items:
|
||||
value = item.value
|
||||
if item.sep == SEP_COMMON:
|
||||
target = headers
|
||||
else:
|
||||
if not stdin_isatty:
|
||||
parser.error('Request body (stdin) and request '
|
||||
'data (key=value) cannot be mixed.')
|
||||
if item.sep == SEP_DATA_RAW:
|
||||
try:
|
||||
value = json.loads(item.value)
|
||||
except ValueError:
|
||||
if args.traceback:
|
||||
raise
|
||||
parser.error('%s:=%s is not valid JSON'
|
||||
% (item.key, item.value))
|
||||
target = data
|
||||
target[item.key] = item.value
|
||||
target[item.key] = value
|
||||
|
||||
if not stdin_isatty:
|
||||
data = stdin.read()
|
||||
@ -171,7 +183,7 @@ def main(args=None,
|
||||
sys.stderr.write(str(e.message) + '\n')
|
||||
sys.exit(1)
|
||||
|
||||
# Display the response.
|
||||
# Reconstruct the raw response.
|
||||
encoding = response.encoding or 'ISO-8859-1'
|
||||
original = response.raw._original_response
|
||||
status_line, headers, body = (
|
||||
@ -186,11 +198,12 @@ def main(args=None,
|
||||
if do_prettify:
|
||||
prettify = pretty.PrettyHttp(args.style)
|
||||
if args.print_headers:
|
||||
status_line = prettify.headers(status_line).strip()
|
||||
status_line = prettify.headers(status_line)
|
||||
headers = prettify.headers(headers)
|
||||
if args.print_body and 'content-type' in response.headers:
|
||||
body = prettify.body(body, response.headers['content-type'])
|
||||
if args.print_body and 'Content-Type' in response.headers:
|
||||
body = prettify.body(body, response.headers['Content-Type'])
|
||||
|
||||
# Output.
|
||||
if args.print_headers:
|
||||
stdout.write(status_line)
|
||||
stdout.write('\n')
|
||||
|
4
setup.py
4
setup.py
@ -5,13 +5,13 @@ import httpie
|
||||
|
||||
requirements = ['requests>=0.10.4', 'Pygments>=1.4']
|
||||
|
||||
if sys.version_info < (2 , 7):
|
||||
if sys.version_info < (2, 7):
|
||||
requirements.append('argparse>=1.2.1')
|
||||
|
||||
|
||||
setup(name='httpie',version=httpie.__version__,
|
||||
description=httpie.__doc__.strip(),
|
||||
url='https://github.com/jkbr/httpie',
|
||||
url='http://httpie.org/',
|
||||
author=httpie.__author__,
|
||||
license=httpie.__licence__,
|
||||
packages=['httpie'],
|
||||
|
Loading…
Reference in New Issue
Block a user