1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Add body_format for handling of JSON and YAML body

body_format is a new optional argument that enables handling of JSON or
YAML serialization format for the body argument.
When set to either 'json' or 'yaml', the body argument can be a dict or list.
The body will be encoded, and the Content-Type HTTP header will be set,
accordingly to the body_format argument.

Example:

- name: Facette - Create memory graph
  uri:
    method: POST
    url: http://facette/api/v1/library/graphs
    status_code: 201
    body_format: json
    body:
      name: "{{ ansible_fqdn }} - Memory usage"
      attributes:
        Source": "{{ ansible_fqdn }}"
        link: "1947a490-8ac6-4bf2-47c1-ff74272f8b32"
This commit is contained in:
Sébastien Bocahu 2015-03-27 11:11:25 +01:00 committed by Matt Clay
parent 5224375a5a
commit 4af3b60167

View file

@ -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, json, or yaml. When set to json or yaml, 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,7 +243,7 @@ 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
@ -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', 'yaml']),
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,8 +374,18 @@ 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 or yaml, encore 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'
elif body_format == 'yaml':
body = yaml.dump(body)
dict_headers['Content-Type'] = 'application/yaml'
# 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_", "")
@ -400,7 +417,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