diff --git a/lib/ansible/modules/network/basics/uri.py b/lib/ansible/modules/network/basics/uri.py index 24257dc356..882f3fd5d8 100644 --- a/lib/ansible/modules/network/basics/uri.py +++ b/lib/ansible/modules/network/basics/uri.py @@ -65,6 +65,8 @@ options: description: - The serialization format of the body. When set to json, encodes the body argument, if needed, and automatically sets the Content-Type header accordingly. + As of C(2.3) it is possible to override the `Content-Type` header, when + set to json via the I(headers) option. required: false choices: [ "raw", "json" ] default: raw @@ -134,7 +136,9 @@ options: default: null headers: description: - - Add custom HTTP headers to a request in the format of a YAML hash + - Add custom HTTP headers to a request in the format of a YAML hash. As + of C(2.3) supplying C(Content-Type) here will override the header + generated by supplying C(json) for I(body_format). required: false default: null version_added: '2.1' @@ -399,7 +403,9 @@ def main(): # Encode the body unless its a string, then assume it is pre-formatted JSON if not isinstance(body, basestring): body = json.dumps(body) - dict_headers['Content-Type'] = 'application/json' + lower_header_keys = [key.lower() for key in dict_headers] + if 'content-type' not in lower_header_keys: + 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"}') diff --git a/test/integration/targets/uri/tasks/main.yml b/test/integration/targets/uri/tasks/main.yml index 68757c0f82..c282ddfc76 100644 --- a/test/integration/targets/uri/tasks/main.yml +++ b/test/integration/targets/uri/tasks/main.yml @@ -280,3 +280,16 @@ status_code: 202 method: POST body: foo + +- name: Validate body_format json does not override content-type in 2.3 or newer + uri: + url: "https://{{ httpbin_host }}/post" + method: POST + body: + foo: bar + body_format: json + headers: + 'Content-Type': 'text/json' + return_content: true + register: result + failed_when: result.json.headers['Content-Type'] != 'text/json'