requests: InvalidHeader exception when trying to add a ":method" header field (breaking change in 2.28.0)

My app is making a POST request enforcing some specific header fields. Among them, I was able to explicitly enforce a “:method” header field. But since 2.28.0, this now raises the following error:

requests.exceptions.InvalidHeader: Invalid leading whitespace, reserved character(s), or returncharacter(s) in header name: ':method'

Enforcing this header field is possibly a widespread practice as this is part of the requirements for sending notifications through Apple’s push notification servers.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 6
  • Comments: 16 (4 by maintainers)

Most upvoted comments

replace in header ‘\r’ or ‘\t’ or ‘\n’ to empty symbol, this solved my problem

Traceback (most recent call last):
  File "C:\Users\abira\Desktop\Bkash Marchent API\app.py", line 52, in <module>
    print(api.TableData())
  File "C:\Users\abira\Desktop\Bkash Marchent API\app.py", line 44, in TableData
    data = AUTH.post('https://cpp.bka.sh/merchant-portal-backend/merchant/dashboard', json={})
  File "C:\Users\abira\Desktop\Bkash Marchent API\env\lib\site-packages\requests\sessions.py", line 635, in post
    return self.request("POST", url, data=data, json=json, **kwargs)
  File "C:\Users\abira\Desktop\Bkash Marchent API\env\lib\site-packages\requests\sessions.py", line 573, in request
    prep = self.prepare_request(req)
  File "C:\Users\abira\Desktop\Bkash Marchent API\env\lib\site-packages\requests\sessions.py", line 484, in prepare_request
    p.prepare(
  File "C:\Users\abira\Desktop\Bkash Marchent API\env\lib\site-packages\requests\models.py", line 369, in prepare
    self.prepare_headers(headers)
  File "C:\Users\abira\Desktop\Bkash Marchent API\env\lib\site-packages\requests\models.py", line 491, in prepare_headers
    check_header_validity(header)
  File "C:\Users\abira\Desktop\Bkash Marchent API\env\lib\site-packages\requests\utils.py", line 1042, in check_header_validity
    _validate_header_part(name, "name", HEADER_VALIDATORS[type(name)][0])
  File "C:\Users\abira\Desktop\Bkash Marchent API\env\lib\site-packages\requests\utils.py", line 1048, in _validate_header_part
    raise InvalidHeader(
requests.exceptions.InvalidHeader: Invalid leading whitespace, reserved character(s), or returncharacter(s) in header name: ':authority'

This is what shows in my error

Did you guys found any solutions

For those arriving here, this is not a frequently encountered issue, but the solution is in Nate’s comment which I will quote here:

After a cursory look, I don’t think we can escape this at the adapter level. The immediate fix is to use the PreparedRequest workflow in the Requests docs and set the headers after the Request is prepared. This can be handed to the Session and should work.

I’d like to see how often hyper is being used with Requests still before we look at merging in a code change to bypass this.

Please read this and the documentation and do not post comments with “+1” or asking for updates. Further comments like this will result in locking the discussion

After a cursory look, I don’t think we can escape this at the adapter level. The immediate fix is to use the PreparedRequest workflow in the Requests docs and set the headers after the Request is prepared. This can be handed to the Session and should work.

I’d like to see how often hyper is being used with Requests still before we look at merging in a code change to bypass this.