Skip to content
Myr

Merge pull request #1823 from Lukasa/moredocs

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

Update docs to highlight the value of r.encoding.

authored by Kenneth Reitz · committed by Kenneth Reitz · parents e0ba49e, 4a6a0c3 · Browse files · 2 files+15 added−5 removed
This commit

State

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

Diff stats

2 files+15 added−5 removed

  • other:+15 added−5 removed

2 files+15 added−5 removed

modified:docs/user/quickstart.rst+10 added-5 removed
97 unmodified lines
98 'utf-8'99 >>> r.encoding = 'ISO-8859-1'100101If you change the encoding, Requests will use the new value of ``r.encoding``102whenever you call ``r.text``.102whenever you call ``r.text``. You might want to do this in any situation where103you can apply special logic to work out what the encoding of the content will104be. For example, HTTP and XML have the ability to specify their encoding in105their body. In situations like this, you should use ``r.content`` to find the106encoding, and then set ``r.encoding``. This will let you use ``r.text`` with107the correct encoding.108109Requests will also use custom encodings in the event that you need them. If110you have created your own encoding and registered it with the ``codecs``111module, you can simply use the codec name as the value of ``r.encoding`` and
44 unmodified lines
156 <requests.packages.urllib3.response.HTTPResponse object at 0x101194810>157 >>> r.raw.read(10)158 '\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'159155In general, however, you should use a pattern like this to save what is being 160In general, however, you should use a pattern like this to save what is being161streamed to a file::162163 with open(filename, 'wb') as fd:164 for chunk in r.iter_content(chunk_size):165 fd.write(chunk)166162Using ``Response.iter_content`` will handle a lot of what you would otherwise 163have to handle when using ``Response.raw`` directly. When streaming a 164download, the above is the preferred and recommended way to retrieve the 167Using ``Response.iter_content`` will handle a lot of what you would otherwise168have to handle when using ``Response.raw`` directly. When streaming a169download, the above is the preferred and recommended way to retrieve the170content.171172173Custom Headers
262 unmodified lines
modified:requests/models.py+5 added
690 unmodified lines
691 """Content of the response, in unicode.692693 If Response.encoding is None, encoding will be guessed using694 ``chardet``.695696 The encoding of the response content is determined based soley on HTTP697 headers, following RFC 2616 to the letter. If you can take advantage of698 non-HTTP knowledge to make a better guess at the encoding, you should699 set ``r.encoding`` appropriately before accessing this property.700 """701702 # Try charset from content-type703 content = None
78 unmodified lines