Advertisement
peter9477

hot-patch python requests 1.2.3 for Python 3.3

Sep 1st, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. Python requests package 1.2.3 on Python 3.x has an issue when used with custom headers, and possibly only when sending binary data (data=b'bytes'). This hotpatches it, and works here in testing when I'm trying to set headers={'Content-Type': 'my-custom-content-type'} in the .post() call.
  2.  
  3.        from requests.models import PreparedRequest
  4.        from requests.structures import CaseInsensitiveDict
  5.  
  6.        # hot-patch requests to avoid problem with
  7.        def prepare_headers(self, headers):
  8.            if headers:
  9.                headers = dict((name, value) for name, value in headers.items())
  10.                self.headers = CaseInsensitiveDict(headers)
  11.            else:
  12.                self.headers = CaseInsensitiveDict()
  13.  
  14.        PreparedRequest.prepare_headers = prepare_headers
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement