Only add Content-Type if not specified in headers. Fixes #20046 (#20234)

* Only add Content-Type if not specified in headers. Fixes #20046

* Update documentation to indicate body_format will not override Content-Type if specified in headers
This commit is contained in:
Matt Martz 2017-01-13 12:19:38 -06:00 committed by Brian Coca
parent 0988de8c4c
commit d25a70846f
2 changed files with 21 additions and 2 deletions

View file

@ -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"}')

View file

@ -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'