Merge pull request #1011 from zecrazytux/bugreport_265
Add body_format for handling of JSON and YAML body
This commit is contained in:
commit
66a996184e
1 changed files with 20 additions and 6 deletions
|
@ -64,6 +64,11 @@ options:
|
||||||
- The body of the http request/response to the web service.
|
- The body of the http request/response to the web service.
|
||||||
required: false
|
required: false
|
||||||
default: null
|
default: null
|
||||||
|
body_format:
|
||||||
|
description:
|
||||||
|
- The serialization format of the body. Either raw, or json. When set to json, encodes the body argument and automatically sets the Content-Type header accordingly.
|
||||||
|
required: false
|
||||||
|
default: raw
|
||||||
method:
|
method:
|
||||||
description:
|
description:
|
||||||
- The HTTP method of the request or response.
|
- The HTTP method of the request or response.
|
||||||
|
@ -238,11 +243,11 @@ def url_filename(url):
|
||||||
return fn
|
return fn
|
||||||
|
|
||||||
|
|
||||||
def uri(module, url, dest, user, password, body, method, headers, redirects, socket_timeout):
|
def uri(module, url, dest, user, password, body, body_format, method, headers, redirects, socket_timeout):
|
||||||
# To debug
|
# To debug
|
||||||
#httplib2.debug = 4
|
#httplib2.debug = 4
|
||||||
|
|
||||||
# Handle Redirects
|
# Handle Redirects
|
||||||
if redirects == "all" or redirects == "yes":
|
if redirects == "all" or redirects == "yes":
|
||||||
follow_redirects = True
|
follow_redirects = True
|
||||||
follow_all_redirects = True
|
follow_all_redirects = True
|
||||||
|
@ -335,6 +340,7 @@ def main():
|
||||||
user = dict(required=False, default=None),
|
user = dict(required=False, default=None),
|
||||||
password = dict(required=False, default=None),
|
password = dict(required=False, default=None),
|
||||||
body = dict(required=False, default=None),
|
body = dict(required=False, default=None),
|
||||||
|
body_format = dict(required=False, default='raw', choices=['raw', 'json']),
|
||||||
method = dict(required=False, default='GET', choices=['GET', 'POST', 'PUT', 'HEAD', 'DELETE', 'OPTIONS', 'PATCH']),
|
method = dict(required=False, default='GET', choices=['GET', 'POST', 'PUT', 'HEAD', 'DELETE', 'OPTIONS', 'PATCH']),
|
||||||
return_content = dict(required=False, default='no', type='bool'),
|
return_content = dict(required=False, default='no', type='bool'),
|
||||||
force_basic_auth = dict(required=False, default='no', type='bool'),
|
force_basic_auth = dict(required=False, default='no', type='bool'),
|
||||||
|
@ -357,6 +363,7 @@ def main():
|
||||||
user = module.params['user']
|
user = module.params['user']
|
||||||
password = module.params['password']
|
password = module.params['password']
|
||||||
body = module.params['body']
|
body = module.params['body']
|
||||||
|
body_format = module.params['body_format']
|
||||||
method = module.params['method']
|
method = module.params['method']
|
||||||
dest = module.params['dest']
|
dest = module.params['dest']
|
||||||
return_content = module.params['return_content']
|
return_content = module.params['return_content']
|
||||||
|
@ -367,14 +374,21 @@ def main():
|
||||||
status_code = [int(x) for x in list(module.params['status_code'])]
|
status_code = [int(x) for x in list(module.params['status_code'])]
|
||||||
socket_timeout = module.params['timeout']
|
socket_timeout = module.params['timeout']
|
||||||
|
|
||||||
# Grab all the http headers. Need this hack since passing multi-values is currently a bit ugly. (e.g. headers='{"Content-Type":"application/json"}')
|
|
||||||
dict_headers = {}
|
dict_headers = {}
|
||||||
|
|
||||||
|
# If body_format is json, encodes the body (wich can be a dict or a list) and automatically sets the Content-Type header
|
||||||
|
if body_format == 'json':
|
||||||
|
body = json.dumps(body)
|
||||||
|
dict_headers['Content-Type'] = 'application/json'
|
||||||
|
|
||||||
|
|
||||||
|
# Grab all the http headers. Need this hack since passing multi-values is currently a bit ugly. (e.g. headers='{"Content-Type":"application/json"}')
|
||||||
for key, value in module.params.iteritems():
|
for key, value in module.params.iteritems():
|
||||||
if key.startswith("HEADER_"):
|
if key.startswith("HEADER_"):
|
||||||
skey = key.replace("HEADER_", "")
|
skey = key.replace("HEADER_", "")
|
||||||
dict_headers[skey] = value
|
dict_headers[skey] = value
|
||||||
|
|
||||||
|
|
||||||
if creates is not None:
|
if creates is not None:
|
||||||
# do not run the command if the line contains creates=filename
|
# do not run the command if the line contains creates=filename
|
||||||
# and the filename already exists. This allows idempotence
|
# and the filename already exists. This allows idempotence
|
||||||
|
@ -400,7 +414,7 @@ def main():
|
||||||
|
|
||||||
|
|
||||||
# Make the request
|
# Make the request
|
||||||
resp, content, dest = uri(module, url, dest, user, password, body, method, dict_headers, redirects, socket_timeout)
|
resp, content, dest = uri(module, url, dest, user, password, body, body_format, method, dict_headers, redirects, socket_timeout)
|
||||||
resp['status'] = int(resp['status'])
|
resp['status'] = int(resp['status'])
|
||||||
|
|
||||||
# Write the file out if requested
|
# Write the file out if requested
|
||||||
|
@ -422,7 +436,7 @@ def main():
|
||||||
# Transmogrify the headers, replacing '-' with '_', since variables dont work with dashes.
|
# Transmogrify the headers, replacing '-' with '_', since variables dont work with dashes.
|
||||||
uresp = {}
|
uresp = {}
|
||||||
for key, value in resp.iteritems():
|
for key, value in resp.iteritems():
|
||||||
ukey = key.replace("-", "_")
|
ukey = key.replace("-", "_")
|
||||||
uresp[ukey] = value
|
uresp[ukey] = value
|
||||||
|
|
||||||
if 'content_type' in uresp:
|
if 'content_type' in uresp:
|
||||||
|
|
Loading…
Reference in a new issue