Flask Restful accept list in mail request

I use Flask Restful for my server API and send a dictionary to the server, where one of the values ​​is a list of dictionaries.

parser.add_argument('products_in_basket', type=list) def post(self, user_id): args = parser.parse_args() print request.data print args['my_list'] 

I have a problem: args ['my_list'] returns only the first element of the list. While I see the whole list from request.data.

This is request.data p>

 {"address_id":1,"my_list":[{"size":12,"colour":"red","id":34219,"quantity":1},{"size":10,"colour":"red","id":12219,"quantity":2},{"size":8,"colour":"red","id":5214,"quantity":3}],"payment_card_id":1} 

This is args ['my_list']

 [u'colour', u'quantity', u'id', u'size'] 

Where am I going wrong?

+6
source share
2 answers

What are the options for your add_argument ? Is products_in_basket actual key for the requested data? Or are you trying to provide an arbitrary name and / or rename a dict?

Take a look at the Multiple Values ​​and Lists from the Query Documentation.

You might want to do something like this ...

 parser = reqparse.RequestParser() parser.add_argument('my_list', action='append') 
+7
source

Try to access the following data:

 for product in args.my_list: size = product.get('size') etc.... 

This will allow you to iterate over all the dict objects in the my_list .

+1
source

All Articles