Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Jesse Shapiro · committed by Jesse Shapiro · parent 989e8f1 · Browse files · 3 files+18 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+18 added−2 removed

  • tests:+12 added−0 removed
  • other:+6 added−2 removed

3 files+18 added−2 removed

modified:requests/models.py+5 added-2 removed
418 unmodified lines
419 content_type = None420 length = None421422 if not data and json is not None:423 # When urllib3 uses pyOpenSSL, it can only resume large uploads424 # properly if receiving a bytes-like object. In Python 2, json.dumps()425 # returns just that, but Python 3 returns a Unicode string.423 content_type = 'application/json'424 body = complexjson.dumps(json)426 content_type = 'application/json; charset=utf-8'427 body = complexjson.dumps(json).encode('utf-8')428429 is_stream = all([430 hasattr(data, '__iter__'),431 not isinstance(data, (basestring, list, tuple, dict))
427 unmodified lines
modified:tests/test_requests.py+12 added
1514 unmodified lines
1515 setattr(string, 'release_conn', lambda *args: args)1516 return string151715181519def test_json_encodes_as_bytes():1520 # urllib3 expects bodies as bytes-like objects1521 body = {"key": "value"}1522 p = PreparedRequest()1523 p.prepare(1524 method='GET',1525 url='https://www.example.com/',1526 json='body'1527 )1528 assert isinstance(p.body, bytes)152915301531def test_requests_are_updated_each_time(httpbin):1532 session = RedirectSession([303, 307])1533 prep = requests.Request('POST', httpbin('post')).prepare()1534 r0 = session.send(prep)
94 unmodified lines
modified:AUTHORS.rst+1 added
163 unmodified lines
164- Robin Linderborg (`@vienno <https://github.com/vienno>`_)165- Brian Samek (`@bsamek <https://github.com/bsamek>`_)166- Dmitry Dygalo (`@Stranger6667 <https://github.com/Stranger6667>`_)167- piotrjurkiewicz168- Jesse Shapiro <jesse@jesseshapiro.net> (`@haikuginger <https://github.com/haikuginger>`_)