mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
Encoding JSON requests to bytes for urllib3 to handle; ensuring same with testing.
This commit is contained in:
@@ -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
@@ -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__'),
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user