0

I have such a route:

@app.route('/wikidata/api/v1.0/ask', methods=['POST'])
def get_tasks():

    print(request.data)
    print(request.json)
    return jsonify(1)

I send a request:

curl -i -H "Content-Type:application/json" -X POST -d "{\"название\": \"значение?\",\"param1\": \"Q29424\"}" http://localhost:8529/wikidata/api/v1.0/ask

and get the error:

HTTP/1.0 400 BAD REQUEST
Content-Type: text/html
Content-Length: 223
Server: Werkzeug/0.14.1 Python/3.6.5
Date: Fri, 15 Feb 2019 08:34:27 GMT

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>Failed to decode JSON object: 'utf-8' codec can't decode byte 0xed in position 2: invalid continuation byte</p>

Meanwhile print(request.data) shows that request.data is b'{"\xed\xe0\xe7\xe2\xe0\xed\xe8\xe5": "\xe7\xed\xe0\xf7\xe5\xed\xe8\xe5?","param1": "Q29424"}'

The only thing that helped so far is

decoded_data = request.data.decode('windows-1251')
question = json.loads(decoded_data)

I'm looking a way to send a request properly (or configure server) so that I can use request.json without errors.

Thank you.

elfinorr
  • 189
  • 3
  • 12

1 Answers1

-1

That's a Windows specific issue likely due to the default charset in the Windows console prompt. The cyrillic characters in your command line are misinterpreted with a non UTF-8 compatible charset.

Since you already use Python, the easiest way IMHO to send your request is to use the requests module (that you can install using pip install requests). Then type this command in a python file, using UTF-8 as charset:

import requests
requests.post("http://localhost:8529/wikidata/api/v1.0/ask", json={"название": "значение?","param1": "Q29424"})

Run it and it will have the same effect as your curl command line, only with proper cyrillic characters handling.

Guillaume
  • 5,497
  • 3
  • 24
  • 42