histrio/py-couchdb

use .encode('utf-8') makes requests library add a new Content-Type Header

KodjoSuprem opened this issue · 4 comments

In Python 3 byte / str problem

requests/models
the folowing code tests if isinstance(data, str) or isinstance(data, builtin_str) or hasattr(data, 'read'):

if files:
            (body, content_type) = self._encode_files(files, data)
        else:
            if data:
                body = self._encode_params(data)
                if isinstance(data, str) or isinstance(data, builtin_str) or hasattr(data, 'read'):
                    content_type = None
                else:
                    content_type = 'application/x-www-form-urlencoded'

        self.prepare_content_length(body)

        # Add content-type if it wasn't explicitly provided.
        if (content_type) and (not 'content-type' in self.headers):
            self.headers['Content-Type'] = content_type

the self.headers dict will contain

b'Content-Type' : b'application/json'
'Content-Type' :  'application/x-www-form-urlencoded'

resulting a couchdb protocol error:

{"error":"bad_content_type","reason":"Content-Type must be application/json"}

Today I can not get time for this project, but tomorrow I'll fix this bug and few others, and I'll upload a new version.

Thank you very much for reporting these bugs!

Hmm that is the version of requests you are used for this test?

(Seems 1.2 is sligty buggy with python3, py-couchtdb tests pass with 1.1 but 1.2 breaks a lot of tests with python3 -.-)

https://github.com/kennethreitz/requests/issues/1250
https://github.com/kennethreitz/requests/pull/1338

I recomend use 1.1 version of requests because 1.2 breaks api and has incorrect handling of headers with python3

@KodjoSuprem This problem is still relevant?