diff --git a/elasticsearch/transport.py b/elasticsearch/transport.py index ca1a9c33..54dbb2eb 100644 --- a/elasticsearch/transport.py +++ b/elasticsearch/transport.py @@ -265,8 +265,8 @@ class Transport(object): if body is not None: try: body = body.encode('utf-8') - except UnicodeDecodeError: - # Python 2 and str, no need to re-encode + except (UnicodeDecodeError, AttributeError): + # bytes/str - no need to re-encode pass ignore = () diff --git a/test_elasticsearch/test_transport.py b/test_elasticsearch/test_transport.py index 9946784f..b95271e9 100644 --- a/test_elasticsearch/test_transport.py +++ b/test_elasticsearch/test_transport.py @@ -81,6 +81,13 @@ class TestTransport(TestCase): self.assertEquals(1, len(t.get_connection().calls)) self.assertEquals(('GET', '/', None, b'\xe4\xbd\xa0\xe5\xa5\xbd'), t.get_connection().calls[0][0]) + def test_body_bytes_get_passed_untouched(self): + t = Transport([{}], connection_class=DummyConnection) + + body = b'\xe4\xbd\xa0\xe5\xa5\xbd' + t.perform_request('GET', '/', body=body) + self.assertEquals(1, len(t.get_connection().calls)) + self.assertEquals(('GET', '/', None, body), t.get_connection().calls[0][0]) def test_kwargs_passed_on_to_connections(self): t = Transport([{'host': 'google.com'}], port=123)