Encoding JSON requests to bytes for urllib3 to handle; ensuring same with testing.

This commit is contained in:
Jesse Shapiro
2016-05-04 20:09:07 -04:00
parent 989e8f15dd
commit 386382b18c
3 changed files with 18 additions and 2 deletions
+1
View File
@@ -165,3 +165,4 @@ Patches and Suggestions
- Brian Samek (`@bsamek <https://github.com/bsamek>`_)
- Dmitry Dygalo (`@Stranger6667 <https://github.com/Stranger6667>`_)
- piotrjurkiewicz
- Jesse Shapiro <jesse@jesseshapiro.net> (`@haikuginger <https://github.com/haikuginger>`_)
+5 -2
View File
@@ -420,8 +420,11 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
length = None
if not data and json is not None:
content_type = 'application/json'
body = complexjson.dumps(json)
# When urllib3 uses pyOpenSSL, it can only resume large uploads
# properly if receiving a bytes-like object. In Python 2, json.dumps()
# returns just that, but Python 3 returns a Unicode string.
content_type = 'application/json; charset=utf-8'
body = complexjson.dumps(json).encode('utf-8')
is_stream = all([
hasattr(data, '__iter__'),
+12
View File
@@ -1516,6 +1516,18 @@ class RedirectSession(SessionRedirectMixin):
return string
def test_json_encodes_as_bytes():
# urllib3 expects bodies as bytes-like objects
body = {"key": "value"}
p = PreparedRequest()
p.prepare(
method='GET',
url='https://www.example.com/',
json='body'
)
assert isinstance(p.body, bytes)
def test_requests_are_updated_each_time(httpbin):
session = RedirectSession([303, 307])
prep = requests.Request('POST', httpbin('post')).prepare()