summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Coca <bcoca@ansible.com>2015-03-30 07:35:17 -0400
committerBrian Coca <bcoca@ansible.com>2015-03-30 07:35:17 -0400
commit66a996184e5914381ef995d05919c5afe3c675e4 (patch)
tree0663fca9ea7aa789a7617dead9d84afcd4eff9bd
parent8c80900fefb7ab2b925bd7f7ccb72d1a5fb47a30 (diff)
parentb2d570e991a01ba4ad15f623998c5acfed4993a8 (diff)
downloadansible-modules-core-66a996184e5914381ef995d05919c5afe3c675e4.tar.gz
Merge pull request #1011 from zecrazytux/bugreport_265
Add body_format for handling of JSON and YAML body
-rw-r--r--network/basics/uri.py26
1 files changed, 20 insertions, 6 deletions
diff --git a/network/basics/uri.py b/network/basics/uri.py
index 9929316d..24f0dbf9 100644
--- a/network/basics/uri.py
+++ b/network/basics/uri.py
@@ -64,6 +64,11 @@ options:
- The body of the http request/response to the web service.
required: false
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:
description:
- The HTTP method of the request or response.
@@ -238,11 +243,11 @@ def url_filename(url):
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
#httplib2.debug = 4
- # Handle Redirects
+ # Handle Redirects
if redirects == "all" or redirects == "yes":
follow_redirects = True
follow_all_redirects = True
@@ -335,6 +340,7 @@ def main():
user = dict(required=False, default=None),
password = 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']),
return_content = 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']
password = module.params['password']
body = module.params['body']
+ body_format = module.params['body_format']
method = module.params['method']
dest = module.params['dest']
return_content = module.params['return_content']
@@ -367,14 +374,21 @@ def main():
status_code = [int(x) for x in list(module.params['status_code'])]
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 = {}
+
+ # 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():
if key.startswith("HEADER_"):
skey = key.replace("HEADER_", "")
dict_headers[skey] = value
-
+
if creates is not None:
# do not run the command if the line contains creates=filename
# and the filename already exists. This allows idempotence
@@ -400,7 +414,7 @@ def main():
# 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'])
# Write the file out if requested
@@ -422,7 +436,7 @@ def main():
# Transmogrify the headers, replacing '-' with '_', since variables dont work with dashes.
uresp = {}
for key, value in resp.iteritems():
- ukey = key.replace("-", "_")
+ ukey = key.replace("-", "_")
uresp[ukey] = value
if 'content_type' in uresp: