Ensure Content-Type header is set when required

This used to be set to application/json when there was request data,
but this was removed in change
I6bd9a0719acfb839fcf137c58bcf03254b1af5ad without any particular
explanation.

There have been reports of some redfish implementations returning
error responses (400 or 415) when this header is not set, specifically
HPE Gen9. It is good practice to always set Content-Type on requests
with data anyway. This change adds that header back.

Change-Id: I13107bbea4d422fdfe620ade735a2e150cb51bf2
This commit is contained in:
Steve Baker 2021-03-08 20:20:07 +13:00
parent d4c7e0395b
commit f52aac46cd
2 changed files with 5 additions and 1 deletions

View File

@ -93,7 +93,10 @@ class Connector(object):
url = path if urlparse.urlparse(path).netloc else urlparse.urljoin(
self._url, path)
headers = headers or {}
if not any(k.lower() == 'odata-version' for k in headers):
lc_headers = [k.lower() for k in headers]
if data is not None and 'content-type' not in lc_headers:
headers['Content-Type'] = 'application/json'
if 'odata-version' not in lc_headers:
headers['OData-Version'] = '4.0'
# TODO(lucasagomes): We should mask the data to remove sensitive
# information

View File

@ -220,6 +220,7 @@ class ConnectorOpTestCase(base.TestCase):
self.conn._session.headers['X-Auth-Token'] = 'asdf1234'
expected_headers = self.headers.copy()
expected_headers['OData-Version'] = '4.0'
expected_headers['Content-Type'] = 'application/json'
self.conn._op('POST', path='fake/path', headers=self.headers,
data=self.data)
self.request.assert_called_once_with(