Can't POST UTF-8 encoded data

Hi,

I'm using Requests to send data to a Firebase database. Data contained in the JSON file is UTF-8 encoded. I'm sending that data as follows:

```
data_to_send = json.dumps(data).encode("utf-8")
request_object = self.requests.put(request_ref, headers=headers, data=data_to_send)
```

But this is sent as "latin-1" text no matter what, because the underlying HTTP client library always sends the body's text with that encoding.

```
        if isinstance(body, str):
            # RFC 2616 Section 3.7.1 says that text default has a
            # default charset of iso-8859-1.
            body = _encode(body, 'body')
```

This results on weird characters displayed on my website.

If instead of:
`data_to_send = json.dumps(data).encode("utf-8")`
or
`data_to_send = json.dumps(data, ensure_ascii=False).encode("utf-8")`
I do this:
`data_to_send = json.dumps(data, ensure_ascii=False)`

Then, HTTP client library throws this error:

```
"client.py", line 1150, in _send_request
    body = _encode(body, 'body')
  File "C:\Users\mribeiro\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 161, in _encode
    (name.title(), data[err.start:err.end], name)) from None
UnicodeEncodeError: 'latin-1' codec can't encode character '\u2030' in position 164: Body ('▒') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.
```

So, how am I supposed to POST UTF-8 encoded data using Requests?


@cidadao You'll need to more explicitly state what your actual problem is. Let me address why.

Firstly, you say you want to POST your JSON as UTF-8 encoded data. Why is this needed? By default, the Python JSON library will automatically escape all non-ASCII unicode code points. For example:

``` python
Python 3.5.1 (default, Dec 14 2015, 09:21:15) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> data = {u'key': u'恒写価時究討昭名会証仕玲予邸庁子等図者'}
>>> json.dumps(data)
'{"key": "\\u6052\\u5199\\u4fa1\\u6642\\u7a76\\u8a0e\\u662d\\u540d\\u4f1a\\u8a3c\\u4ed5\\u73b2\\u4e88\\u90b8\\u5e81\\u5b50\\u7b49\\u56f3\\u8005"}
```

This code produces the same result on Python 2.7, by the way. I mention this because it doesn't matter if this is encoded in UTF-8 or Latin-1, the exact output will be identical as it uses _only ASCII characters_.

Your next comment is about apparent problems with data encoding. I cannot reproduce this problem:

``` python
>>> requests.post('http://http2bin.org/post', data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
<Response [200]>
```

This is where I use the same data dictionary as above, containing a whole slew of Chinese characters. Wireshark reveals the posted body as being the byte sequence `7b 22 6b 65 79 22 3a 20 22 e6 81 92 e5 86 99 e4 be a1 e6 99 82 e7 a9 b6 e8 a8 8e e6 98 ad e5 90 8d e4 bc 9a e8 a8 bc e4 bb 95 e7 8e b2 e4 ba 88 e9 82 b8 e5 ba 81 e5 ad 90 e7 ad 89 e5 9b b3 e8 80 85 22 7d`, which decodes via UTF-8 to:

``` python
>>> my_data = "7b 22 6b 65 79 22 3a 20 22 e6 81 92 e5 86 99 e4 be a1 e6 99 82 e7 a9 b6 e8 a8 8e e6 98 ad e5 90 8d e4 bc 9a e8 a8 bc e4 bb 95 e7 8e b2 e4 ba 88 e9 82 b8 e5 ba 81 e5 ad 90 e7 ad 89 e5 9b b3 e8 80 85 22 7d"
>>> import binascii
>>> binascii.unhexlify(my_data.replace(' ', '')).decode('utf-8')
'{"key": "恒写価時究討昭名会証仕玲予邸庁子等図者"}'
```

So I think I need more information. What does your data look like? What do your headers look like? What Python and Requests version are you using?


Thanks for the detailed follow up! It turned out to be much more simple than what I thought and totally my fault.
My fault: not explicitly opening the JSON file with `encoding="utf-8"` 
`fd = open(file, "r", encoding="utf-8")`

Using that and sending data with: 

```
data_to_send = json.dumps(data).encode("utf-8")
request_object = self.requests.put(request_ref, headers=headers, data=data_to_send)
```

Solved the problem!