Anton Putau
Anton Putau

Reputation: 762

Access bottle post parameters

I am posting two post parameters with node js to bottle service using axios library

var axios = require('axios')

axios.post('http://localhost:8080/filter-tags', {
   xml: 'Fred',
   tags: 'Flintstone'
})
.then(function (response) {
    console.log(response);
})
.catch(function (error) {
    console.log(error);
});

My bottle service is

from bottle import route, run, request
from pprint import pprint

@route('/filter-tags', method='POST')
def filterTags():
    xml1 = request.POST.xml
    xml2 = request.forms.get('xml')
    print(pprint(vars(request.POST)))
    print('-->' + xml1 +'<----')
    print(xml2)
    return 'trololo'

run(host='localhost', port=8080, debug=True)

My output is

 {'dict': {'{"xml":"Fred","tags":"Flintstone"}': ['']}} 
 None  // don't know why this value appears here
 --><----  // xml1
 None // xml2

Here is link to bottle docs, but I see no my mistake

Any ideas how to access parameters? Thanks.

Upvotes: 0

Views: 518

Answers (1)

Raja Simon
Raja Simon

Reputation: 10305

By default, axios serializes JavaScript objects to JSON. I believe you're using the Node.js library. Please consider use querystring to send application/x-www-form-urlencoded format

var querystring = require('querystring');
axios.post('http://localhost:8080/filter-tags', querystring.stringify({ 
   xml: 'Fred',
   tags: 'Flintstone'

}));

By doing this you will get {'dict': {'xml': ['Fred'], 'tags': ['Flintstone']}} which is very easy to get the datas. In this case just request.POST.xml

Upvotes: 1

Related Questions