Search code examples
pythonjsonpostpyramid

Python Pyramid parse json


I'm posting json to a Python Pyramid server, but I can't parse it on the server side.

The post request look like this:

$.ajax({
url: "http://localhost:6543/linefollower/7/send_result", 
type: "POST",
data: '{"results": [{"robot_name": "Satikas", "result": null, "team_nr": 30, "team_name": "IT Vennad", "id": 57}]}',
contentType: "application/json; charset=utf-8",
dataType: "json"
}

But on the server side I'm receiving this when I do print(request.body)

b'%5B%7B%22robot_name%22%3A+%22Satikas%22%2C+%22result%22%3A+null%2C+%22team_nr%22%3A+30%2C+%22team_name%22%3A+%22IT+Vennad%22%2C+%22id%22%3A+57%7D%5D='

What should I do to be able to parse the posted content as JSON? When should Pyramid's request.json_body contain the parsed json?


Solution

  • Try explicitly serializing the data as JSON before sending it:

    $.ajax({
        url: "http://localhost:6543/linefollower/7/send_result", 
        type: "POST",
        data: JSON.stringify({"results": [... "team_name": "IT Vennad", "id": 57}]}),
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }