psf / requests

A simple, yet elegant, HTTP library.
https://requests.readthedocs.io/en/latest/
Apache License 2.0
52.16k stars 9.33k forks source link

Requests with german umlauts go failed #2657

Closed OlafRadicke closed 9 years ago

OlafRadicke commented 9 years ago

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)

Lukasa commented 9 years ago

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:

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)
OlafRadicke commented 9 years ago

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

Lukasa commented 9 years ago

You can find it here. You can also find it in the documentation.

OlafRadicke commented 9 years ago

Thank you. That is very helpful.