Skip to content
Myr

Merge pull request #3678 from nateprewitt/3675_streaming_docs

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

decoding fix for Streaming Requests docs

authored by Cory Benfield · committed by GitHub · parents 70f1f8e, d81ad06 · Browse files · 1 file+15 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

1 file+15 added−0 removed

  • other:+15 added−0 removed

1 file+15 added−0 removed

modified:docs/user/advanced.rst+15 added
494 unmodified lines
495 for line in r.iter_lines():496497 # filter out keep-alive new lines498 if line:499 decoded_line = line.decode('utf-8')500 print(json.loads(decoded_line))501502When using `decode_unicode=True` with503:meth:`Response.iter_lines() <requests.Response.iter_lines>` or504:meth:`Response.iter_content() <requests.Response.iter_content>`, you'll want505to provide a fallback encoding in the event the server doesn't provide one::506507 r = requests.get('http://httpbin.org/stream/20', stream=True)508509 if r.encoding is None:510 r.encoding = 'utf-8'511512 for line in r.iter_lines(decode_unicode=True):513 if line:514 print(json.loads(line))515516.. warning::517
472 unmodified lines