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

Added --headers and --body to limit the output.

This commit is contained in:
Jakub Roztočil 2012-02-26 01:37:28 +01:00
parent 65a2fc7042
commit 5653b9c6a0
2 changed files with 39 additions and 16 deletions

View File

@ -6,7 +6,7 @@ import argparse
from collections import namedtuple
import requests
from requests.structures import CaseInsensitiveDict
from .pretty import prettify
from . import pretty
__author__ = 'Jakub Roztocil'
@ -46,17 +46,26 @@ parser = argparse.ArgumentParser(
# Content type.
group = parser.add_mutually_exclusive_group(required=False)
group.add_argument('--json', '-j', action='store_true',
group_type = parser.add_mutually_exclusive_group(required=False)
group_type.add_argument('--json', '-j', action='store_true',
help='Serialize data items as a JSON object and set'
' Content-Type to application/json, if not specified.')
group.add_argument('--form', '-f', action='store_true',
group_type.add_argument('--form', '-f', action='store_true',
help='Serialize data items as form values and set'
' Content-Type to application/x-www-form-urlencoded,'
' if not specified.')
# Output options.
parser.add_argument('--ugly', '-u', help='Do not prettify the response.',
dest='prettify', action='store_false', default=True)
group_only = parser.add_mutually_exclusive_group(required=False)
group_only.add_argument('--headers', '-t', dest='print_body',
action='store_false', default=True,
help='Print only the response headers.')
group_only.add_argument('--body', '-b', dest='print_headers',
action='store_false', default=True,
help='Print only the response body.')
# ``requests.request`` keyword arguments.
parser.add_argument('--auth', help='username:password',
@ -136,7 +145,7 @@ def main():
# Display the response.
original = response.raw._original_response
response_bits = (
status_line, headers, body = (
u'HTTP/{version} {status} {reason}'.format(
version='.'.join(str(original.version)),
status=original.status, reason=original.reason,
@ -146,10 +155,20 @@ def main():
)
if args.prettify and sys.stdout.isatty():
response_bits = prettify(response.headers['content-type'], *response_bits)
print u'\n'.join(response_bits)
if args.print_headers:
status_line = pretty.prettify_http(status_line)
headers = pretty.prettify_http(headers)
if args.print_body:
body = pretty.prettify_body(body,
response.headers['content-type'])
if args.print_headers:
print status_line
print headers
if args.print_body:
if args.print_headers:
print
print body
if __name__ == '__main__':
main()

View File

@ -28,22 +28,26 @@ highlight = partial(pygments.highlight,
highlight_http = partial(highlight, lexer=HTTPLexer())
def prettify(content_type, status_line, headers, body):
content_type = content_type.split(';')[0]
def prettify_http(headers):
return highlight_http(headers)[:-1]
def prettify_body(content, content_type):
content_type = content_type.split(';')[0]
if 'json' in content_type:
content_type = TYPE_JS
try:
# Indent JSON
body = json.dumps(json.loads(body), sort_keys=True, indent=4)
content = json.dumps(json.loads(content),
sort_keys=True, indent=4)
except Exception:
pass
try:
body = highlight(code=body, lexer=get_lexer_for_mimetype(content_type))
lexer = get_lexer_for_mimetype(content_type)
content = highlight(code=content, lexer=lexer)
if content:
content = content[:-1]
except Exception:
pass
return content
return (highlight_http(code=status_line).strip(),
highlight_http(code=headers),
body)