From d0ecd9e3a19319a64cb8a48e4dad73fd9819528c Mon Sep 17 00:00:00 2001 From: "D.S. Ljungmark" Date: Wed, 17 Jun 2015 14:25:17 +0200 Subject: [PATCH] request-certificate: Handle JSON formatted responses We have moved over to using JSON formatted error messages, rather than html templates. This fixes issue #26 --- .../caramelrequest/certificaterequest.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/request-certificate/caramelrequest/certificaterequest.py b/request-certificate/caramelrequest/certificaterequest.py index e9c5beb..881f071 100755 --- a/request-certificate/caramelrequest/certificaterequest.py +++ b/request-certificate/caramelrequest/certificaterequest.py @@ -168,7 +168,7 @@ def request_cert_from_server(self): break else: logging.error('Request failed: {}' - .format(parse_html(response))) + .format(parse(response))) response.raise_for_status() break @@ -182,6 +182,14 @@ def printerr(text): sys.stderr.write(text + '\n') +def parse(response): + try: + result = response.json() + except Exception: + result = parse_html(response) + return result + + def parse_html(response): return ''.join((e.text or '') + (e.tail or '') for e in ET.fromstring(response.text).iterfind('body//'))