Why does requests ignore my custom header field?

Viewed 84

I have the following Python function which sends a post request using the requests library:

def http_post(self, url: str, headers: dict, data: str, auth: AuthBase):
        token = self._xsuaa.get_token(self._service)
        headers.update({'Proxy-Authorization': f"Bearer {token}"})
        res = requests.post(
            url,
            headers=headers,
            data=data,
            proxies={'http': self._proxy},
            auth=auth,
            verify=False,
            timeout=100,
            allow_redirects=True)

When printing the headers dict, it looks like this:

{
    'Content-Type': 'multipart/mixed;boundary=batch_4724f345-bb46-437d-a970-197a7b82bf41',
    'Content-Transfer-Encoding': 'binary',
    'sap-cancel-on-close': 'true',
    'sap-contextid-accept': 'header',
    'Accept': 'application/json',
    'Accept-Language': 'de-DE',
    'DataServiceVersion': '2.0',
    'MaxDataServiceVersion': '2.0',
    'Proxy-Authorization': 'Bearer <token>'
}

However, when I take a look at res.request.headers, I get the following:

{
    'User-Agent': 'python-requests/2.26.0',
    'Accept-Encoding': 'gzip, deflate',
    'Accept': 'application/json',
    'Connection': 'keep-alive',
    'Content-Type': 'multipart/mixed; boundary=batch_4724f345-bb46-437d-a970-197a7b82bf41',
    'Content-Transfer-Encoding': 'binary',
    'sap-cancel-on-close': 'true',
    'sap-contextid-accept': 'header',
    'Accept-Language': 'de-DE',
    'DataServiceVersion': '2.0',
    'MaxDataServiceVersion': '2.0',
    'Content-Length': '659',
    'Authorization': 'Basic <auth>'
}

For some reason, the proxy-authorization header field is gone and accordingly, I get a 407 error in the response. I have read in the documentation that proxy credentials provided in the URL overwrite proxy-authorization headers, but my URL contains none. I also tried removing the auth=auth line from the request, but the problem still persited. Can someone point me in the right direction as to why this field is seemingly ignored or overwritten by requests?

0 Answers
Related