From 2f569b901dcd5852d16a88084af19883ee847f3e Mon Sep 17 00:00:00 2001 From: Alen Mujezinovic Date: Tue, 28 Feb 2012 14:01:01 +0000 Subject: [PATCH] Not all web servers return UTF-8 and will crash httpie when decoding the response --- httpie/httpie.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/httpie/httpie.py b/httpie/httpie.py index 2588c8a1..55ef83c1 100755 --- a/httpie/httpie.py +++ b/httpie/httpie.py @@ -163,13 +163,22 @@ def main(): # Display the response. original = response.raw._original_response + + try: + decode_from = [ + bit.split('=')[1] for bit in response.headers['content-type'].split(';') + if 'charset' in bit + ][0] + except IndexError: + decode_from = 'utf-8' + status_line, headers, body = ( u'HTTP/{version} {status} {reason}'.format( version='.'.join(str(original.version)), status=original.status, reason=original.reason, ), - str(original.msg).decode('utf-8'), - response.content.decode('utf-8') if response.content else u'' + str(original.msg).decode(decode_from), + response.content.decode(decode_from) if response.content else u'' ) if args.prettify and sys.stdout.isatty():