Merge pull request #3178 from haikuginger/master

Encoding JSON requests to bytes for urllib3 to handle
This commit is contained in:
Cory Benfield
2016-05-22 17:02:09 +01:00
3 changed files with 17 additions and 0 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>`_)
+4
View File
@@ -420,8 +420,12 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
length = None
if not data and json is not None:
# urllib3 requires a bytes-like body. Python 2's json.dumps
# provides this natively, but Python 3 gives a Unicode string.
content_type = 'application/json'
body = complexjson.dumps(json)
if not isinstance(body, bytes):
body = body.encode('utf-8')
is_stream = all([
hasattr(data, '__iter__'),
+12
View File
@@ -1528,6 +1528,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()