with fhe following code
payload = '''工作报告 总体情况:良好
'''
r = requests.post("http://httpbin.org/post", data=payload)
what is the default encoding when Requests post data is string type? UTF8 or unicode-escape?
if I like to specify a encoding type, do I have to encode it myself and pass a bytes object to parameter 'data'?
As per latest JSON spec (RFC-8259) when using external services you must encode your JSON payloads as UTF-8. Here is a quick solution:
r = requests.post("http://httpbin.org/post", data=payload.encode('utf-8'))
requests
uses httplib
which defaults to latin-1
encoding. Byte arrays aren't automatically encoded so it is always better encode your text data yourself and use a bytearray.
I'd also recommend to set the charset using the headers
parameter:
r = requests.post("http://httpbin.org/post", data=payload.encode('utf-8'),headers={'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'})