Skip to content
Myr

documentation on encoding fallback for streaming

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

authored by Nate Prewitt · committed by Nate Prewitt · parent 71b8941 · Browse files · 1 file+14 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+14 added−0 removed

  • other:+14 added−0 removed

1 file+14 added−0 removed

modified:docs/user/advanced.rst+14 added
497 unmodified lines
498 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::517518 :meth:`~requests.Response.iter_lines()` is not reentrant safe.519 Calling this method multiple times causes some of the received data
470 unmodified lines