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)
replace in header ‘\r’ or ‘\t’ or ‘\n’ to empty symbol, this solved my problem
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:
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.