<p dir="auto">Using  requests 2.13.0 I can't seem to pass a non-ascii value as "data" on a PUT.</p>
<p dir="auto">This might be a duplicate of issue <a href="https://github.com/kennethreitz/requests/issues/2638">https://github.com/kennethreitz/requests/issues/2638</a>.</p>
<p dir="auto">How should I send a unicode string in a request body? Doing an <code class="notranslate">.encode('utf-8')</code> seems to workaround the problem in first example, but that still fails in the second.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt;AUTH = requests.auth.HTTPBasicAuth(&quot;john&quot;, &quot;password&quot;)
&gt;&gt;&gt;requests.put('http://localhost:8084/',headers={'Content-type':'text/plain; charset=utf-8'}, data=u'\u24B6\u24B7\u24B8\u24B9',auth=AUTH)
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/usr/lib/python2.7/site-packages/requests/api.py&quot;, line 124, in put
    return request('put', url, data=data, **kwargs)
  File &quot;/usr/lib/python2.7/site-packages/requests/api.py&quot;, line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File &quot;/usr/lib/python2.7/site-packages/requests/sessions.py&quot;, line 488, in request
    resp = self.send(prep, **send_kwargs)
  File &quot;/usr/lib/python2.7/site-packages/requests/sessions.py&quot;, line 609, in send
    r = adapter.send(request, **kwargs)
  File &quot;/usr/lib/python2.7/site-packages/requests/adapters.py&quot;, line 423, in send
    timeout=timeout
  File &quot;/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py&quot;, line 600, in urlopen
    chunked=chunked)
  File &quot;/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py&quot;, line 356, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File &quot;/usr/lib64/python2.7/httplib.py&quot;, line 1020, in request
    self._send_request(method, url, body, headers)
  File &quot;/usr/lib64/python2.7/httplib.py&quot;, line 1054, in _send_request
    self.endheaders(body)
  File &quot;/usr/lib64/python2.7/httplib.py&quot;, line 1016, in endheaders
    self._send_output(message_body)
  File &quot;/usr/lib64/python2.7/httplib.py&quot;, line 871, in _send_output
    self.send(message_body)
  File &quot;/usr/lib64/python2.7/httplib.py&quot;, line 840, in send
    self.sock.sendall(data)
  File &quot;/usr/lib64/python2.7/socket.py&quot;, line 224, in meth
    return getattr(self._sock,name)(*args)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)"><pre class="notranslate"><code class="notranslate">&gt;&gt;&gt;AUTH = requests.auth.HTTPBasicAuth("john", "password")
&gt;&gt;&gt;requests.put('http://localhost:8084/',headers={'Content-type':'text/plain; charset=utf-8'}, data=u'\u24B6\u24B7\u24B8\u24B9',auth=AUTH)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/usr/lib/python2.7/site-packages/requests/api.py", line 124, in put
    return request('put', url, data=data, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/adapters.py", line 423, in send
    timeout=timeout
  File "/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 356, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib64/python2.7/httplib.py", line 1020, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib64/python2.7/httplib.py", line 1054, in _send_request
    self.endheaders(body)
  File "/usr/lib64/python2.7/httplib.py", line 1016, in endheaders
    self._send_output(message_body)
  File "/usr/lib64/python2.7/httplib.py", line 871, in _send_output
    self.send(message_body)
  File "/usr/lib64/python2.7/httplib.py", line 840, in send
    self.sock.sendall(data)
  File "/usr/lib64/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
</code></pre></div>
<p dir="auto">Btw the same PUT with AWS S3 auth (V2 or V4) has similar error but different stack (I added the "!!!" prints for debugging):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; AUTH=AWS4Auth('john', 'password', 'eu-west-1', 's3')
&gt;&gt;&gt; requests.put('http://localhost:8084/',headers={'Content-type':'text/plain'}, data=u'\u24B6\u24B7\u24B8\u24B9',auth=AUTH)
!!!1 u'PUT / HTTP/1.1\r\nHost: localhost:8084\r\nConnection: keep-alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.13.0\r\ncontent-type: text/plain; charset=utf-8\r\nContent-Length: 12\r\nx-amz-date: 20170215T023125Z\r\nx-amz-content-sha256: 7ec37a06579472c0743b58bd45af589cca817f65bbd8c6e528bc5e3092166396\r\nAuthorization: AWS4-HMAC-SHA256 Credential=john/20170215/eu-west-1/s3/aws4_request, SignedHeaders=content-type;host;x-amz-content-sha256;x-amz-date, Signature=e83f7e17d7c6e25a940965962df32e9c690681b158f6d9ad9f484bf9c09bb963\r\n\r\n'
!!!2 '\xe2\x92\xb6\xe2\x92\xb7\xe2\x92\xb8\xe2\x92\xb9'
!!!3 u'PUT / HTTP/1.1\r\nHost: localhost:8084\r\nConnection: keep-alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.13.0\r\ncontent-type: text/plain; charset=utf-8\r\nContent-Length: 12\r\nx-amz-date: 20170215T023125Z\r\nx-amz-content-sha256: 7ec37a06579472c0743b58bd45af589cca817f65bbd8c6e528bc5e3092166396\r\nAuthorization: AWS4-HMAC-SHA256 Credential=john/20170215/eu-west-1/s3/aws4_request, SignedHeaders=content-type;host;x-amz-content-sha256;x-amz-date, Signature=e83f7e17d7c6e25a940965962df32e9c690681b158f6d9ad9f484bf9c09bb963\r\n\r\n\u24b6\u24b7\u24b8\u24b9'
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/usr/lib/python2.7/site-packages/requests/api.py&quot;, line 124, in put
    return request('put', url, data=data, **kwargs)
  File &quot;/usr/lib/python2.7/site-packages/requests/api.py&quot;, line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File &quot;/usr/lib/python2.7/site-packages/requests/sessions.py&quot;, line 488, in request
    resp = self.send(prep, **send_kwargs)
  File &quot;/usr/lib/python2.7/site-packages/requests/sessions.py&quot;, line 609, in send
    r = adapter.send(request, **kwargs)
  File &quot;/usr/lib/python2.7/site-packages/requests/adapters.py&quot;, line 423, in send
    timeout=timeout
  File &quot;/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py&quot;, line 600, in urlopen
    chunked=chunked)
  File &quot;/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py&quot;, line 356, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File &quot;/usr/lib64/python2.7/httplib.py&quot;, line 1020, in request
    self._send_request(method, url, body, headers)
  File &quot;/usr/lib64/python2.7/httplib.py&quot;, line 1054, in _send_request
    self.endheaders(body)
  File &quot;/usr/lib64/python2.7/httplib.py&quot;, line 1016, in endheaders
    self._send_output(message_body)
  File &quot;/usr/lib64/python2.7/httplib.py&quot;, line 865, in _send_output
    msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)"><pre class="notranslate"><code class="notranslate">&gt;&gt;&gt; AUTH=AWS4Auth('john', 'password', 'eu-west-1', 's3')
&gt;&gt;&gt; requests.put('http://localhost:8084/',headers={'Content-type':'text/plain'}, data=u'\u24B6\u24B7\u24B8\u24B9',auth=AUTH)
!!!1 u'PUT / HTTP/1.1\r\nHost: localhost:8084\r\nConnection: keep-alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.13.0\r\ncontent-type: text/plain; charset=utf-8\r\nContent-Length: 12\r\nx-amz-date: 20170215T023125Z\r\nx-amz-content-sha256: 7ec37a06579472c0743b58bd45af589cca817f65bbd8c6e528bc5e3092166396\r\nAuthorization: AWS4-HMAC-SHA256 Credential=john/20170215/eu-west-1/s3/aws4_request, SignedHeaders=content-type;host;x-amz-content-sha256;x-amz-date, Signature=e83f7e17d7c6e25a940965962df32e9c690681b158f6d9ad9f484bf9c09bb963\r\n\r\n'
!!!2 '\xe2\x92\xb6\xe2\x92\xb7\xe2\x92\xb8\xe2\x92\xb9'
!!!3 u'PUT / HTTP/1.1\r\nHost: localhost:8084\r\nConnection: keep-alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.13.0\r\ncontent-type: text/plain; charset=utf-8\r\nContent-Length: 12\r\nx-amz-date: 20170215T023125Z\r\nx-amz-content-sha256: 7ec37a06579472c0743b58bd45af589cca817f65bbd8c6e528bc5e3092166396\r\nAuthorization: AWS4-HMAC-SHA256 Credential=john/20170215/eu-west-1/s3/aws4_request, SignedHeaders=content-type;host;x-amz-content-sha256;x-amz-date, Signature=e83f7e17d7c6e25a940965962df32e9c690681b158f6d9ad9f484bf9c09bb963\r\n\r\n\u24b6\u24b7\u24b8\u24b9'
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/usr/lib/python2.7/site-packages/requests/api.py", line 124, in put
    return request('put', url, data=data, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/adapters.py", line 423, in send
    timeout=timeout
  File "/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 356, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib64/python2.7/httplib.py", line 1020, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib64/python2.7/httplib.py", line 1054, in _send_request
    self.endheaders(body)
  File "/usr/lib64/python2.7/httplib.py", line 1016, in endheaders
    self._send_output(message_body)
  File "/usr/lib64/python2.7/httplib.py", line 865, in _send_output
    msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)
</code></pre></div>
<p dir="auto">Python version: 2.7.12 and 3.5.2<br>
Requests version: 2.11.1</p>
<p dir="auto">I'm sorry if this is a duplicate, but I don't know what the issue is, so I also don't even know how to look for it in the search... But this might be a bug, so I decided to report.</p>
<p dir="auto">When I run this in Python 2:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import requests


response = requests.post('http://evds.tcmb.gov.tr/cgi-bin/famecgi', data={
    'cgi': '$ozetweb',
    'ARAVERIGRUP': 'bie_yymkpyuk.db',
    'DIL': 'UK',
    'ONDALIK': '5',
    'wfmultiple_selection': 'ZAMANSERILERI',
    'f_begdt': '07-01-2005',
    'f_enddt': '07-10-2016',
    'ZAMANSERILERI': ['TP.PYUK1', 'TP.PYUK2', 'TP.PYUK21', 'TP.PYUK22', 'TP.PYUK3', 'TP.PYUK4', 'TP.PYUK5', 'TP.PYUK6'],
    'YON': '3',
    'SUBMITDEG': 'Report',
    'GRTYPE': '1',
    'EPOSTA': 'xxx',
    'RESIMPOSTA': '***',
})

print(response.text)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">requests</span>


<span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-s1">requests</span>.<span class="pl-en">post</span>(<span class="pl-s">'http://evds.tcmb.gov.tr/cgi-bin/famecgi'</span>, <span class="pl-s1">data</span><span class="pl-c1">=</span>{
    <span class="pl-s">'cgi'</span>: <span class="pl-s">'$ozetweb'</span>,
    <span class="pl-s">'ARAVERIGRUP'</span>: <span class="pl-s">'bie_yymkpyuk.db'</span>,
    <span class="pl-s">'DIL'</span>: <span class="pl-s">'UK'</span>,
    <span class="pl-s">'ONDALIK'</span>: <span class="pl-s">'5'</span>,
    <span class="pl-s">'wfmultiple_selection'</span>: <span class="pl-s">'ZAMANSERILERI'</span>,
    <span class="pl-s">'f_begdt'</span>: <span class="pl-s">'07-01-2005'</span>,
    <span class="pl-s">'f_enddt'</span>: <span class="pl-s">'07-10-2016'</span>,
    <span class="pl-s">'ZAMANSERILERI'</span>: [<span class="pl-s">'TP.PYUK1'</span>, <span class="pl-s">'TP.PYUK2'</span>, <span class="pl-s">'TP.PYUK21'</span>, <span class="pl-s">'TP.PYUK22'</span>, <span class="pl-s">'TP.PYUK3'</span>, <span class="pl-s">'TP.PYUK4'</span>, <span class="pl-s">'TP.PYUK5'</span>, <span class="pl-s">'TP.PYUK6'</span>],
    <span class="pl-s">'YON'</span>: <span class="pl-s">'3'</span>,
    <span class="pl-s">'SUBMITDEG'</span>: <span class="pl-s">'Report'</span>,
    <span class="pl-s">'GRTYPE'</span>: <span class="pl-s">'1'</span>,
    <span class="pl-s">'EPOSTA'</span>: <span class="pl-s">'xxx'</span>,
    <span class="pl-s">'RESIMPOSTA'</span>: <span class="pl-s">'***'</span>,
})

<span class="pl-en">print</span>(<span class="pl-s1">response</span>.<span class="pl-s1">text</span>)</pre></div>
<p dir="auto">I always get the same response, consistently, no matter how many times. If I run this in Python 3, I get a different response. I checked the request body being sent to the server, and the request headers, and they are all the same in both versions of Python. The correct response is obtained from running this in Python 2.</p>
<p dir="auto">Since both versions of Python are supported, and the same API is exposed, then the same result should happen. I also asked this in Stack Overflow, and at least one other developer was able to reprodeuce and confirm this happens: <a href="http://stackoverflow.com/questions/40118133/library-requests-getting-different-results-unpredictably" rel="nofollow">http://stackoverflow.com/questions/40118133/library-requests-getting-different-results-unpredictably</a></p>
<p dir="auto">You should check the discussion at Stack Overflow, as some people entertained some more scenarios.</p>