liv2hak
liv2hak

Reputation: 14970

Accessing HTTP POST data from Bottle Server

I have a simple python http listening server as shown below.

from bottle import route, run

@route('/',method='POST')
def default():
    return 'My first bottle program.'

run(host='192.168.132.125', port=1729)

If I do a POST of some data (a JSON file) from another server as follows

curl -X POST -d @data_100 http://192.168.132.125:1729/

I get the output

My First Bottle Program

Now I want my Bottle server to dump the contents of the posted JSON file to a folder on the server.How can I achieve this.

Upvotes: 2

Views: 3398

Answers (2)

ron rothman
ron rothman

Reputation: 18128

You may want to look at Bottle's built-in json property.

With no error checking, it'd look something like this:

@route('/', method='POST')
def default():
    json_text = request.json
    with open('/path/to/file', 'wb') as f:
        f.write(json_text)
    return 'My first bottle program.'

Upvotes: 2

Alex Pertsev
Alex Pertsev

Reputation: 948

You can access posted form data with request.forms object. And then parse/dump/do_anything with standard python tools.

You can read about FormDict and his methods here: http://bottlepy.org/docs/dev/api.html#bottle.FormsDict

Upvotes: 0

Related Questions