Skip to content

Instantly share code, notes, and snippets.

@jbd
Created May 16, 2011 16:16
Show Gist options
  • Save jbd/974757 to your computer and use it in GitHub Desktop.
Save jbd/974757 to your computer and use it in GitHub Desktop.
Couchdbkit and unicode
#!/usr/bin/env python
# -*- coding: utf8 -*-
# couchdbkit.__version__ '0.5.4'
# restkit.__version__ '3.2.3'
# couchdb - Apache CouchDB '1.0.2'
import couchdbkit
import restkit
server = couchdbkit.Server()
db = server.get_or_create_db('unicode_test')
unicodes_str = [ u'abc_\u03a0', u'àéç', u'pure_Π' ]
unicodes_str_utf8 = [ s.encode('utf8') for s in unicodes_str ]
print "- Python unicode untouched -"
for s in unicodes_str:
try:
db.save_doc({'data':s})
print s, "saved in couchdb"
except restkit.errors.RequestFailed, err:
print "Cannot save", s, "in couchdb", err
print "- Python unicode utf8 encoded -"
for s in unicodes_str_utf8:
try:
db.save_doc({'data':s})
print s, "saved in couchdb"
except restkit.errors.RequestFailed, err:
print "Cannot save", s, "in couchdb:", err
# PYTHON OUTPUT
# restkit.errors.RequestFailed: {"error":"bad_request","reason":"invalid UTF-8 JSON"}
# COUCHDB LOG
#[error] [<0.103.0>] attempted upload of invalid JSON (set log_level to debug to log it)
#[debug] [<0.103.0>] Invalid JSON: <<"{\"data\": \"abc\\u03a0\"}">>
#[info] [<0.103.0>] 127.0.0.1 - - 'PUT' /unicode_test/73c60a002219e61e05c9a0acb01fcf22 400
#[debug] [<0.103.0>] httpd 400 error response:
# {"error":"bad_request","reason":"invalid UTF-8 JSON"}
#
#[debug] [<0.103.0>] 'POST' /unicode_test {1,1}
#Headers: [{'Accept',"application/json"},
# {'Accept-Encoding',"identity"},
# {'Content-Length',"21"},
# {'Content-Type',"application/json"},
# {'Host',"127.0.0.1:5984"},
# {'User-Agent',"restkit/3.2.3"}]
#[debug] [<0.103.0>] OAuth Params: []
#[error] [<0.103.0>] attempted upload of invalid JSON (set log_level to debug to log it)
#[debug] [<0.103.0>] Invalid JSON: <<"{\"data\": \"abc\\u03a0\"}">>
#[info] [<0.103.0>] 127.0.0.1 - - 'POST' /unicode_test 400
#[debug] [<0.103.0>] httpd 400 error response:
# {"error":"bad_request","reason":"invalid UTF-8 JSON"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment