Make sure uri output contains json output when a non-200 status is returned

Prior to the switch to the urls.py code, non-200 responses contained
a 'json' value when the content-type was JSON. This fix restores that
field upon a non-2xx response.

Fixes ansible/ansible#15555
This commit is contained in:
James Cammarata 2016-04-25 14:19:03 -04:00 committed by Matt Clay
parent 75dbe9f1e8
commit 50cd8b0aa5

View file

@ -325,13 +325,18 @@ def uri(module, url, dest, body, body_format, method, headers, socket_timeout):
resp, info = fetch_url(module, url, data=body, headers=headers, resp, info = fetch_url(module, url, data=body, headers=headers,
method=method, timeout=socket_timeout) method=method, timeout=socket_timeout)
r['redirected'] = redirected or info['url'] != url
r.update(redir_info)
r.update(info)
try: try:
content = resp.read() content = resp.read()
except AttributeError: except AttributeError:
content = '' # there was no content, but the error read()
# may have been stored in the info as 'body'
content = info.pop('body', '')
r['redirected'] = redirected or info['url'] != url
r.update(redir_info)
r.update(info)
return r, content, dest return r, content, dest
@ -440,14 +445,15 @@ def main():
if 'charset' in params: if 'charset' in params:
content_encoding = params['charset'] content_encoding = params['charset']
u_content = unicode(content, content_encoding, errors='replace') u_content = unicode(content, content_encoding, errors='replace')
if content_type.startswith('application/json') or \ if 'application/json' in content_type or 'text/json' in content_type:
content_type.startswith('text/json'):
try: try:
js = json.loads(u_content) js = json.loads(u_content)
uresp['json'] = js uresp['json'] = js
except: except:
module.fail_json(msg="bombed")
pass pass
else: else:
module.fail_json(msg="boo")
u_content = unicode(content, content_encoding, errors='replace') u_content = unicode(content, content_encoding, errors='replace')
if resp['status'] not in status_code: if resp['status'] not in status_code: