2012-02-25 13:39:38 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import sys
|
|
|
|
import json
|
2012-03-04 11:29:55 +01:00
|
|
|
try:
|
|
|
|
from collections import OrderedDict
|
|
|
|
except ImportError:
|
|
|
|
OrderedDict = dict
|
2012-03-15 00:11:49 +01:00
|
|
|
import requests
|
2012-04-25 00:08:40 +02:00
|
|
|
from requests.compat import urlparse, str, is_py3
|
2012-02-25 13:39:38 +01:00
|
|
|
from requests.structures import CaseInsensitiveDict
|
2012-03-04 10:48:30 +01:00
|
|
|
from . import cli
|
2012-02-26 01:37:28 +01:00
|
|
|
from . import pretty
|
2012-02-28 17:14:31 +01:00
|
|
|
from . import __version__ as version
|
2012-02-25 13:39:38 +01:00
|
|
|
|
|
|
|
|
2012-03-15 00:11:49 +01:00
|
|
|
NEW_LINE = str('\n')
|
2012-02-28 17:14:31 +01:00
|
|
|
DEFAULT_UA = 'HTTPie/%s' % version
|
2012-02-25 13:39:38 +01:00
|
|
|
TYPE_FORM = 'application/x-www-form-urlencoded; charset=utf-8'
|
|
|
|
TYPE_JSON = 'application/json; charset=utf-8'
|
|
|
|
|
|
|
|
|
2012-03-14 00:05:44 +01:00
|
|
|
class HTTPMessage(object):
|
|
|
|
|
|
|
|
def __init__(self, line, headers, body, content_type=None):
|
|
|
|
# {Request,Status}-Line
|
|
|
|
self.line = line
|
|
|
|
self.headers = headers
|
|
|
|
self.body = body
|
|
|
|
self.content_type = content_type
|
|
|
|
|
|
|
|
|
|
|
|
def format_http_message(message, prettifier=None,
|
|
|
|
with_headers=True, with_body=True):
|
|
|
|
bits = []
|
|
|
|
if with_headers:
|
|
|
|
if prettifier:
|
|
|
|
bits.append(prettifier.headers(message.line))
|
|
|
|
bits.append(prettifier.headers(message.headers))
|
|
|
|
else:
|
|
|
|
bits.append(message.line)
|
|
|
|
bits.append(message.headers)
|
2012-03-14 19:30:58 +01:00
|
|
|
if with_body and message.body:
|
2012-03-15 00:11:49 +01:00
|
|
|
bits.append(NEW_LINE)
|
2012-03-14 19:30:58 +01:00
|
|
|
if with_body and message.body:
|
2012-03-14 00:05:44 +01:00
|
|
|
if prettifier and message.content_type:
|
|
|
|
bits.append(prettifier.body(message.body, message.content_type))
|
|
|
|
else:
|
|
|
|
bits.append(message.body)
|
2012-03-15 00:11:49 +01:00
|
|
|
bits.append(NEW_LINE)
|
|
|
|
return NEW_LINE.join(bit.strip() for bit in bits)
|
2012-03-14 00:05:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
def make_request_message(request):
|
|
|
|
"""Make an `HTTPMessage` from `requests.models.Request`."""
|
|
|
|
url = urlparse(request.url)
|
|
|
|
request_headers = dict(request.headers)
|
2012-03-14 01:10:28 +01:00
|
|
|
if 'Host' not in request_headers:
|
|
|
|
request_headers['Host'] = url.netloc
|
2012-03-14 00:05:44 +01:00
|
|
|
return HTTPMessage(
|
|
|
|
line='{method} {path} HTTP/1.1'.format(
|
|
|
|
method=request.method,
|
2012-03-14 19:30:12 +01:00
|
|
|
path=url.path or '/'),
|
2012-03-15 00:11:49 +01:00
|
|
|
headers=NEW_LINE.join(str('%s: %s') % (name, value)
|
2012-03-14 00:05:44 +01:00
|
|
|
for name, value
|
2012-03-15 00:11:49 +01:00
|
|
|
in request_headers.items()),
|
2012-03-14 00:05:44 +01:00
|
|
|
body=request._enc_data,
|
|
|
|
content_type=request_headers.get('Content-Type')
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def make_response_message(response):
|
|
|
|
"""Make an `HTTPMessage` from `requests.models.Response`."""
|
|
|
|
encoding = response.encoding or 'ISO-8859-1'
|
|
|
|
original = response.raw._original_response
|
|
|
|
response_headers = response.headers
|
|
|
|
return HTTPMessage(
|
|
|
|
line='HTTP/{version} {status} {reason}'.format(
|
|
|
|
version='.'.join(str(original.version)),
|
|
|
|
status=original.status, reason=original.reason,),
|
2012-03-15 00:11:49 +01:00
|
|
|
headers=str(original.msg),
|
|
|
|
body=response.content.decode(encoding) if response.content else '',
|
2012-03-14 00:05:44 +01:00
|
|
|
content_type=response_headers.get('Content-Type'))
|
|
|
|
|
|
|
|
|
2012-03-02 01:40:40 +01:00
|
|
|
def main(args=None,
|
|
|
|
stdin=sys.stdin,
|
2012-03-02 02:36:21 +01:00
|
|
|
stdin_isatty=sys.stdin.isatty(),
|
2012-03-02 01:40:40 +01:00
|
|
|
stdout=sys.stdout,
|
|
|
|
stdout_isatty=sys.stdout.isatty()):
|
|
|
|
|
2012-03-04 10:48:30 +01:00
|
|
|
parser = cli.parser
|
|
|
|
|
2012-03-02 01:40:40 +01:00
|
|
|
args = parser.parse_args(args if args is not None else sys.argv[1:])
|
2012-03-04 01:36:39 +01:00
|
|
|
do_prettify = (args.prettify is True or
|
2012-03-14 00:05:44 +01:00
|
|
|
(args.prettify == cli.PRETTIFY_STDOUT_TTY_ONLY
|
|
|
|
and stdout_isatty))
|
2012-03-04 10:48:30 +01:00
|
|
|
|
2012-02-25 13:39:38 +01:00
|
|
|
# Parse request headers and data from the command line.
|
|
|
|
headers = CaseInsensitiveDict()
|
|
|
|
headers['User-Agent'] = DEFAULT_UA
|
2012-03-04 10:48:30 +01:00
|
|
|
data = OrderedDict()
|
2012-03-14 12:17:39 +01:00
|
|
|
files = OrderedDict()
|
2012-03-04 10:48:30 +01:00
|
|
|
try:
|
2012-03-14 12:17:39 +01:00
|
|
|
cli.parse_items(items=args.items, headers=headers,
|
|
|
|
data=data, files=files)
|
2012-03-04 10:48:30 +01:00
|
|
|
except cli.ParseError as e:
|
|
|
|
if args.traceback:
|
|
|
|
raise
|
|
|
|
parser.error(e.message)
|
2012-02-25 13:39:38 +01:00
|
|
|
|
2012-03-14 12:17:39 +01:00
|
|
|
if files and not args.form:
|
|
|
|
# We could just switch to --form automatically here,
|
|
|
|
# but I think it's better to make it explicit.
|
|
|
|
parser.error(
|
|
|
|
' You need to set the --form / -f flag to'
|
|
|
|
' to issue a multipart request. File fields: %s'
|
|
|
|
% ','.join(files.keys()))
|
|
|
|
|
2012-03-02 01:40:40 +01:00
|
|
|
if not stdin_isatty:
|
2012-03-04 10:48:30 +01:00
|
|
|
if data:
|
|
|
|
parser.error('Request body (stdin) and request '
|
|
|
|
'data (key=value) cannot be mixed.')
|
2012-03-02 01:40:40 +01:00
|
|
|
data = stdin.read()
|
2012-02-25 13:39:38 +01:00
|
|
|
|
|
|
|
# JSON/Form content type.
|
|
|
|
if args.json or (not args.form and data):
|
2012-04-14 21:13:53 +02:00
|
|
|
if args.method.lower() == 'get' and 'Accept' not in headers:
|
|
|
|
headers['Accept'] = 'application/json'
|
2012-03-02 01:40:40 +01:00
|
|
|
if stdin_isatty:
|
2012-02-25 13:39:38 +01:00
|
|
|
data = json.dumps(data)
|
2012-03-14 12:17:39 +01:00
|
|
|
if not files and ('Content-Type' not in headers and (data or args.json)):
|
2012-02-25 13:39:38 +01:00
|
|
|
headers['Content-Type'] = TYPE_JSON
|
2012-03-14 12:17:39 +01:00
|
|
|
elif not files and 'Content-Type' not in headers:
|
2012-02-25 13:39:38 +01:00
|
|
|
headers['Content-Type'] = TYPE_FORM
|
|
|
|
|
|
|
|
# Fire the request.
|
2012-02-26 16:13:12 +01:00
|
|
|
try:
|
2012-03-22 15:40:03 +01:00
|
|
|
credentials = None
|
2012-04-11 13:47:47 +02:00
|
|
|
if args.auth:
|
|
|
|
auth_type = (requests.auth.HTTPDigestAuth
|
|
|
|
if args.auth_type == 'digest'
|
|
|
|
else requests.auth.HTTPBasicAuth)
|
|
|
|
credentials = auth_type(args.auth.key, args.auth.value)
|
2012-03-22 15:40:03 +01:00
|
|
|
|
2012-02-26 16:13:12 +01:00
|
|
|
response = requests.request(
|
|
|
|
method=args.method.lower(),
|
|
|
|
url=args.url if '://' in args.url else 'http://%s' % args.url,
|
|
|
|
headers=headers,
|
|
|
|
data=data,
|
2012-04-11 12:44:02 +02:00
|
|
|
verify={'yes': True, 'no': False}.get(args.verify, args.verify),
|
2012-02-26 16:13:12 +01:00
|
|
|
timeout=args.timeout,
|
2012-03-22 15:40:03 +01:00
|
|
|
auth=credentials,
|
2012-02-28 14:33:33 +01:00
|
|
|
proxies=dict((p.key, p.value) for p in args.proxy),
|
2012-03-14 12:17:39 +01:00
|
|
|
files=files,
|
2012-03-03 20:54:53 +01:00
|
|
|
allow_redirects=args.allow_redirects,
|
2012-02-26 16:13:12 +01:00
|
|
|
)
|
2012-03-04 10:48:30 +01:00
|
|
|
except (KeyboardInterrupt, SystemExit):
|
2012-03-15 00:11:49 +01:00
|
|
|
sys.stderr.write(NEW_LINE)
|
2012-02-26 16:13:12 +01:00
|
|
|
sys.exit(1)
|
|
|
|
except Exception as e:
|
|
|
|
if args.traceback:
|
|
|
|
raise
|
2012-03-15 00:11:49 +01:00
|
|
|
sys.stderr.write(str(e.message) + NEW_LINE)
|
2012-02-26 16:13:12 +01:00
|
|
|
sys.exit(1)
|
2012-02-25 13:39:38 +01:00
|
|
|
|
2012-03-14 00:05:44 +01:00
|
|
|
prettifier = pretty.PrettyHttp(args.style) if do_prettify else None
|
2012-02-25 13:39:38 +01:00
|
|
|
|
2012-04-25 00:08:40 +02:00
|
|
|
do_output_request = (cli.OUT_REQ_HEADERS in args.output_options
|
|
|
|
or cli.OUT_REQ_BODY in args.output_options)
|
2012-03-14 00:05:44 +01:00
|
|
|
|
2012-04-25 00:08:40 +02:00
|
|
|
do_output_response = (cli.OUT_RESP_HEADERS in args.output_options
|
|
|
|
or cli.OUT_RESP_BODY in args.output_options)
|
2012-03-14 00:05:44 +01:00
|
|
|
|
2012-04-25 00:08:40 +02:00
|
|
|
output = []
|
|
|
|
if do_output_request:
|
|
|
|
output.append(format_http_message(
|
2012-03-14 00:05:44 +01:00
|
|
|
message=make_request_message(response.request),
|
|
|
|
prettifier=prettifier,
|
2012-04-25 00:08:40 +02:00
|
|
|
with_headers=cli.OUT_REQ_HEADERS in args.output_options,
|
|
|
|
with_body=cli.OUT_REQ_BODY in args.output_options
|
2012-03-15 00:11:49 +01:00
|
|
|
))
|
2012-04-25 00:08:40 +02:00
|
|
|
if do_output_response:
|
|
|
|
output.append(NEW_LINE)
|
2012-03-14 00:05:44 +01:00
|
|
|
|
2012-04-25 00:08:40 +02:00
|
|
|
if do_output_response:
|
|
|
|
output.append(format_http_message(
|
2012-03-14 00:05:44 +01:00
|
|
|
message=make_response_message(response),
|
|
|
|
prettifier=prettifier,
|
2012-04-25 00:08:40 +02:00
|
|
|
with_headers=cli.OUT_RESP_HEADERS in args.output_options,
|
|
|
|
with_body=cli.OUT_RESP_BODY in args.output_options
|
2012-03-15 00:11:49 +01:00
|
|
|
))
|
2012-04-25 00:08:40 +02:00
|
|
|
output.append(NEW_LINE)
|
|
|
|
|
|
|
|
output_bytes = ''.join(output).encode('utf8')
|
|
|
|
f = (stdout.buffer if is_py3 and hasattr(stdout, 'buffer') else stdout)
|
|
|
|
f.write(output_bytes)
|
2012-02-25 13:39:38 +01:00
|
|
|
|
2012-03-04 11:29:55 +01:00
|
|
|
|
2012-02-25 13:39:38 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|