Requests with german umlauts go failed

Hi, I get trouble with german umlauts in my json requests. An example:

> ^C[or@prag ~]$ python3
> Python 3.4.1 (default, Nov  3 2014, 14:38:10) 
> [GCC 4.9.1 20140930 (Red Hat 4.9.1-11)] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> 
> > > > import requests
> > > > request_uri = "http://127.0.0.1:5984/kochrezepte/pesto_alla_genovese_4"
> > > > headers = {'content-type': 'application/json'}
> > > > json_doc = '{"titel":"Pesto alla genovese","zutaten": ["Basilikum","Knoblauch","Pinienkerne","Käse","Öl","Salz","Pfeffer"], "garzeit": 15 }'
> > > > respon = requests.put(request_uri, auth=("olaf", "olaf"), data=json_doc)
> > > > print( respon.text )
> > > > {"error":"bad_request","reason":"invalid_json"}

Now  without umlauts:

> > > > json_doc = '{"titel":"Pesto alla genovese","zutaten": ["Basilikum","Knoblauch","Pinienkerne","Kaese","Oel","Salz","Pfeffer"], "garzeit": 15 }'
> > > > request_uri = "http://127.0.0.1:5984/kochrezepte/pesto_alla_genovese_5">>> respon = requests.put(request_uri, auth=("olaf", "olaf"), data=json_doc)
> > > > print( respon.text )
> > > > {"ok":true,"id":"pesto_alla_genovese_5","rev":"1-7a7a44a33eace1f7f8b518ffe80bd482"}

With curl I don't get errors:

> [or@prag ~]$ curl -X PUT http://olaf:olaf@127.0.0.1:5984/kochrezepte/pesto_alla_genovese_6 -d '{"titel":"Pesto alla genovese","zutaten": ["Basilikum","Knoblauch","Pinienkerne","Käse","Öl","Salz","Pfeffer"], "garzeit": 15 }'
> {"ok":true,"id":"pesto_alla_genovese_6","rev":"1-cb3146caa4ed3b042803ae184278cc1f"}

CouchDB say:

> [root@prag or]# tail /var/log/couchdb/couch.log
> [Mon, 29 Jun 2015 07:28:04 GMT] [debug] [<0.116.0>] OAuth Params: []
> [Mon, 29 Jun 2015 07:28:04 GMT] [error] [<0.116.0>] attempted upload of invalid JSON (set log_level to debug to log it)
> [Mon, 29 Jun 2015 07:28:04 GMT] [debug] [<0.116.0>] Invalid JSON: {{error,
>                                        {85,
>                                         "lexical error: invalid bytes in UTF8 string.\n"}},
>                                    <<"{\"titel\":\"Pesto alla genovese\",\"zutaten\": [\"Basilikum\",\"Knoblauch\",\"Pinienkerne\",\"K�se\",\"�l\",\"Salz\",\"Pfeffer\"], \"garzeit\": 15 }">>}
> [Mon, 29 Jun 2015 07:28:04 GMT] [info] [<0.116.0>] 127.0.0.1 - - PUT /kochrezepte/pesto_alla_genovese_7 400
> [Mon, 29 Jun 2015 07:28:04 GMT] [debug] [<0.116.0>] httpd 400 error response:
>  {"error":"bad_request","reason":"invalid_json"}

 So it's look like a unicode issue. Is my Python code wrong or the requests lib?

Thank you
Olaf (from germany, augsburg)


I think it's just that your request is wrong. When you use the `data` keyword we send the data as application/x-www-form-urlencoded data, not as JSON. Try using the `json` keyword, like this:

``` python
import requests
request_uri = "http://127.0.0.1:5984/kochrezepte/pesto_alla_genovese_4"
json_doc = '{"titel":"Pesto alla genovese","zutaten": ["Basilikum","Knoblauch","Pinienkerne","Käse","Öl","Salz","Pfeffer"], "garzeit": 15 }'
respon = requests.put(request_uri, auth=("olaf", "olaf"), json=json_doc)
```


Hm, yes it's work...

>   [or@prag ~]$ python3
> Python 3.4.1 (default, Nov  3 2014, 14:38:10) 
> [GCC 4.9.1 20140930 (Red Hat 4.9.1-11)] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> 
> > > import requests
> > > request_uri = "http://127.0.0.1:5984/kochrezepte/pesto_alla_genovese_10"
> > > headers = {'content-type': 'application/json'}
> > > json_doc = '{"titel":"Pesto alla genovese","zutaten": ["Basilikum","Knoblauch","Pinienkerne","Käse","Öl","Salz","Pfeffer"], "garzeit": 15 }'
> > > import json
> > > respon = requests.put(request_uri, auth=("olaf", "olaf"), json=json.loads(json_doc))
> > > print(respon.text)
> > > {"ok":true,"id":"pesto_alla_genovese_10","rev":"1-4367a12038ab5809395199002de7e76d"}

But i can't not find the documentation of this cool feature. And if I look in the sources: https://github.com/kennethreitz/requests/blob/master/requests/api.py than I can only find "data=None". How could I find the?

Thank you

Olaf


You can find it [here](https://github.com/kennethreitz/requests/blob/master/requests/api.py#L24). You can also find it [in the documentation](http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests).


Thank you. That is very helpful.