Encoding problem - or bug in couchdb-0.8-py2.7.egg??
Diez B. Roggisch
deets at web.de
Mon Sep 20 12:00:17 EDT 2010
Ian Hobson <ian at ianhobson.co.uk> writes:
> Hi all,
>
> I have hit a problem and I don't know enough about python to diagnose
> things further. Trying to use couchDB from Python. This script:-
>
> # coding=utf8
> import couchdb
> from couchdb.client import Server
> server = Server()
> dbName = 'python-tests'
> try:
> db = server.create(dbName)
> except couchdb.PreconditionFailed:
> del server[dbName]
> db = server.create(dbName)
> doc_id, doc_rev = db.save({'type': 'Person', 'name': 'John Doe'})
>
> Gives this traceback:-
>
> D:\work\C-U-B>python tes1.py
> Traceback (most recent call last):
> File "tes1.py", line 11, in <module>
> doc_id, doc_rev = db.save({'type': 'Person', 'name': 'John Doe'})
> File
> "C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\client.py",
> line 407, in save
> _, _, data = func(body=doc, **options)
> File
> "C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\http.py",
> line 399, in post_json
> status, headers, data = self.post(*a, **k)
> File
> "C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\http.py",
> line 381, in post
> **params)
> File
> "C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\http.py",
> line 419, in _request
> credentials=self.credentials)
> File
> "C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\http.py",
> line 310, in request
> raise ServerError((status, error))
> couchdb.http.ServerError: (400, ('bad_request', 'invalid UTF-8 JSON'))
>
> D:\work\C-U-B>
>
> Why? I've tried adding u to the strings, and removing the # coding
> line, and I still get the same error.
Sounds cargo-cultish. I suggest you read the python introduction on
unicode.
http://docs.python.org/howto/unicode.html
For your actual problem, I have difficulties seeing how it can happen
with the above data - frankly because there is nothing outside the
ascii-range of data, so there is no reason why anything could be wrong
encoded.
But googling the error-message reveals that there seem to be totally
unrelated reasons for this:
http://sindro.me/2010/4/3/couchdb-invalid-utf8-json
Maybe using something like tcpmon or ethereal to capture the actual
HTTP-request helps to see where the issue comes from.
Diez
More information about the Python-list
mailing list