mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-26 04:11:25 -07:00
* 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:
parent
0988de8c4c
commit
d25a70846f
2 changed files with 21 additions and 2 deletions
|
@ -65,6 +65,8 @@ options:
|
||||||
description:
|
description:
|
||||||
- The serialization format of the body. When set to json, encodes the
|
- The serialization format of the body. When set to json, encodes the
|
||||||
body argument, if needed, and automatically sets the Content-Type header accordingly.
|
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
|
required: false
|
||||||
choices: [ "raw", "json" ]
|
choices: [ "raw", "json" ]
|
||||||
default: raw
|
default: raw
|
||||||
|
@ -134,7 +136,9 @@ options:
|
||||||
default: null
|
default: null
|
||||||
headers:
|
headers:
|
||||||
description:
|
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
|
required: false
|
||||||
default: null
|
default: null
|
||||||
version_added: '2.1'
|
version_added: '2.1'
|
||||||
|
@ -399,6 +403,8 @@ def main():
|
||||||
# Encode the body unless its a string, then assume it is pre-formatted JSON
|
# Encode the body unless its a string, then assume it is pre-formatted JSON
|
||||||
if not isinstance(body, basestring):
|
if not isinstance(body, basestring):
|
||||||
body = json.dumps(body)
|
body = json.dumps(body)
|
||||||
|
lower_header_keys = [key.lower() for key in dict_headers]
|
||||||
|
if 'content-type' not in lower_header_keys:
|
||||||
dict_headers['Content-Type'] = 'application/json'
|
dict_headers['Content-Type'] = 'application/json'
|
||||||
|
|
||||||
# Grab all the http headers. Need this hack since passing multi-values is
|
# Grab all the http headers. Need this hack since passing multi-values is
|
||||||
|
|
|
@ -280,3 +280,16 @@
|
||||||
status_code: 202
|
status_code: 202
|
||||||
method: POST
|
method: POST
|
||||||
body: foo
|
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'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue