Requests POST encoding with python
<p>I'm posting this character "ç" that is usually sent through a form, to a website I don't control. I'm having trouble with the encoding of the data. Trying to send data without encoding it to utf-8 produces this error (correction: this error was for another special character, mistake!):</p>

<pre><code>File "D:\Python34\lib\http\client.py", line 1181, in _send_request
body = body.encode('iso-8859-1')
UnicodeEncodeError: 'latin-1' codec can't encode character '\u0192' in position 512: ordinal not in range(256)
</code></pre>

<p>I'm reading the data from a .csv file saved with utf-8 encoding (is this part of the problem?)</p>

<p>After using .encode('utf-8') and a bit of trial and error:</p>

<p>Posting this: ç Displays on their site as: Ã§</p>

<p>Posting this: Ã§ Displays on their site as: ÃƒÂ§</p>

<p>Sample of shortened code:</p>

<pre><code>headers = {
'Accept' : 'application/json, text/javascript, */*; q=0.01',
'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
}

data = {
"info" : "ç"
}

r = requests.post('www.urlgoeshere.com', headers=headers, data=json.dumps(data,ensure_ascii=False).encode('utf-8'))
</code></pre>

<p>Anyone got any tips on how I can send these special characters and have them display correctly? Or should I just give up and edit them all out. Their website form manages it anyway so I would like to as well.</p>

<p>Edit for more complete .csv code:</p>

<pre><code>with open('data.csv', newline='') as dataFile:
dataReader = csv.reader(dataFile)
for row in dataReader:
    data = {
    "values":{
    "title_id":row[1],
    "title":row[0],
    "other stuff":[{"foo":"bar",
    "too":"foobar"}]}
    }
</code></pre>

<p>It's in that loop that i do the request as well. Data.csv is a comma-delimited file saved as utf-8.</p>