Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I handle a JSON request in Bottle?

I need to get data from JSON, transferred by Ajax from the client. Basically I used something like this:

@route('/ajax')
def serve_ajax():
    return main.parse_request(json.dumps(dict(request.GET)))

Where main.parse_request is a function, that contains some logics to deal with variables in JSON (it is a main procedure of our game engine).

So the problem is that I can't correctly handle JSON variables, by transforming request.GET in a dict: because in a way that I already wrote I can't pass nested objects and arrays. Also every value has a string type, while I need to have integer types on integers and string type on rest other.

Or, since I can obtain the original query string (by request.query_string), how can I convert a query string into an original JSON object?

like image 735
kravitz Avatar asked Oct 30 '10 10:10

kravitz


People also ask

How do JSON requests work?

JSONRequest. post does an HTTP POST of the serialization of a JavaScript object or array, gets the response, and parses the response into a JavaScript value. If the parse is successful, it returns the value to the requesting script. In making the request, no HTTP authentication or cookies are sent.

Can you pass JSON in a GET request?

To get JSON from a REST API endpoint, you must send an HTTP GET request and pass the "Accept: application/json" request header to the server, which will tell the server that the client expects JSON in response.


2 Answers

Now since bottle 0.10, the request.json is ready for use :)

Document is here.

like image 112
Felix Yan Avatar answered Oct 01 '22 14:10

Felix Yan


request.json is limited by MEMFILE_MAX.

Another way works if request data is larger than MEMFILE_MAX

json.load(request.body)
like image 30
Like Avatar answered Oct 01 '22 14:10

Like