How to receive JSON in a POST request in CherryPy?

Python import cherrypy class Root: @cherrypy.expose @cherrypy.tools.json_out() @cherrypy.tools.json_in() def my_route(self): result = {“operation”: “request”, “result”: “success”} input_json = cherrypy.request.json value = input_json[“my_key”] # Responses are serialized to JSON (because of the json_out decorator) return result JavaScript //assuming that you’re using jQuery var myObject = { “my_key”: “my_value” }; $.ajax({ type: “POST”, url: “my_route”, data: JSON.stringify(myObject), … Read more

Python – Flask Default Route possible?

There is a snippet on Flask’s website about a ‘catch-all’ route for flask. You can find it here. Basically the decorator works by chaining two URL filters. The example on the page is: @app.route(“https://stackoverflow.com/”, defaults={‘path’: ”}) @app.route(‘/<path:path>’) def catch_all(path): return ‘You want path: %s’ % path Which would give you: % curl 127.0.0.1:5000 # Matches … Read more

How to get a cross-origin resource sharing (CORS) post request working

I finally stumbled upon this link “A CORS POST request works from plain javascript, but why not with jQuery?” that notes that jQuery 1.5.1 adds the Access-Control-Request-Headers: x-requested-with header to all CORS requests. jQuery 1.5.2 does not do this. Also, according to the same question, setting a server response header of Access-Control-Allow-Headers: * does not … Read more

How to POST JSON data with Python Requests?

Starting with Requests version 2.4.2, you can use the json= parameter (which takes a dictionary) instead of data= (which takes a string) in the call: >>> import requests >>> r = requests.post(‘http://httpbin.org/post’, json={“key”: “value”}) >>> r.status_code 200 >>> r.json() {‘args’: {}, ‘data’: ‘{“key”: “value”}’, ‘files’: {}, ‘form’: {}, ‘headers’: {‘Accept’: ‘*/*’, ‘Accept-Encoding’: ‘gzip, deflate’, ‘Connection’: … Read more