Skip to content
Myr

Merge pull request #3178 from haikuginger/master

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

Encoding JSON requests to bytes for urllib3 to handle

authored by Cory Benfield · committed by Cory Benfield · parents b6d6894, 04bb965 · Browse files · 3 files+17 added−0 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+17 added−0 removed

  • tests:+12 added−0 removed
  • other:+5 added−0 removed

3 files+17 added−0 removed

modified:requests/models.py+4 added
418 unmodified lines
419 content_type = None420 length = None421422 if not data and json is not None:423 # urllib3 requires a bytes-like body. Python 2's json.dumps424 # provides this natively, but Python 3 gives a Unicode string.425 content_type = 'application/json'426 body = complexjson.dumps(json)427 if not isinstance(body, bytes):428 body = body.encode('utf-8')429430 is_stream = all([431 hasattr(data, '__iter__'),432 not isinstance(data, (basestring, list, tuple, dict))
427 unmodified lines
modified:tests/test_requests.py+12 added
1526 unmodified lines
1527 setattr(string, 'release_conn', lambda *args: args)1528 return string152915301531def test_json_encodes_as_bytes():1532 # urllib3 expects bodies as bytes-like objects1533 body = {"key": "value"}1534 p = PreparedRequest()1535 p.prepare(1536 method='GET',1537 url='https://www.example.com/',1538 json=body1539 )1540 assert isinstance(p.body, bytes)154115421543def test_requests_are_updated_each_time(httpbin):1544 session = RedirectSession([303, 307])1545 prep = requests.Request('POST', httpbin('post')).prepare()1546 r0 = session.send(prep)
115 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>`_)