bottle request.json getting a 405 on post bottle request.json getting a 405 on post json json

bottle request.json getting a 405 on post


I think the problem is the server does not allow POST requests. You can probably try sending it in a GET request instead:

urllib2.urlopen("http://location/app/myroute/?" + jdata)

UPDATE:

I just realized, after looking at your question again, that you are actually trying to send JSON data via GET request. You should in general avoid sending JSONs with GET requests, but use POST requests instead[Reference].

To send a POST request to Bottle, you also need to set the headers to application/json:

headers = {}headers['Content-Type'] = 'application/json'jdata = json.dumps({"foo":"bar"})urllib2.urlopen("http://location/app/myroute/", jdata, headers)

Then, with the help of @Anton's answer, you can access the JSON data in your view like this:

@app.post('/myroute/')def myroute():    print request.json

Also, as a bonus, to send a normal GET request and access it:

# send GET requesturllib2.urlopen("http://location/app/myroute/?myvar=" + "test")# access it @app.route('/myroute/')def myroute():    print request.GET['myvar'] # should print "test"


By default, the route decorator makes the decorated function handle only GET requests. You need to add a method argument to tell Bottle to handle POST requests instead. To do that, you need to change:

@app.route('/myroute/') 

to:

@app.route('/myroute/', method='POST')

or a shorter version:

@app.post('/myroute/')