Skip to content

Instantly share code, notes, and snippets.

@juliedavila
Last active January 27, 2016 18:25
Show Gist options
  • Save juliedavila/c7ecc096f1d0f1ba314b to your computer and use it in GitHub Desktop.
Save juliedavila/c7ecc096f1d0f1ba314b to your computer and use it in GitHub Desktop.
requestor
from ansible.module_utils.urls import open_url
import json
def request_json(url, data=None, headers=None, method='GET', use_proxy=True,
force=False, last_mod_time=None, timeout=10, validate_certs=True,
url_username=None, url_password=None, http_agent=None, force_basic_auth=False, ignore_errors=False):
try:
r = open_url(url=url, data=data, headers=headers, method=method, use_proxy=use_proxy,
force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs,
url_username=url_username, url_password=url_password, http_agent=http_agent,
force_basic_auth=force_basic_auth)
except urllib2.HTTPError, err:
r = err.fp
try:
raw_data = r.read()
data = json.loads(raw_data) if raw_data else None
except:
if ignore_errors:
pass
else:
raise
resp_code = r.getcode()
if resp_code >= 400 and not ignore_errors:
raise Exception(resp_code,data)
else:
return resp_code, data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment