address_dict = {'address': {'US': 'San Francisco', 'US': 'New York', 'UK': 'London'}}
When above parameters was sent via requests, how can I get values in address key using request.form on Flask?
import requests
url = 'http://example.com'
params = {"address": {"US": "San Francisco", "UK": "London", "CH": "Shanghai"}}
requests.post(url, data=params)
Then I got this in context of flask.request.
ImmutableMultiDict([('address', u'US'), ('address', 'US'), ('address', 'UK')])
How can I get the value in each key of addresses?
Thanks.
Access Values using get() Another way to access value(s) in a nested dictionary ( employees ) is to use the dict. get() method. This method returns the value for a specified key. If the specified key does not exist, the get() method returns None (preventing a KeyError ).
Adding elements to a Nested Dictionary One way to add a dictionary in the Nested dictionary is to add values one be one, Nested_dict[dict][key] = 'value'. Another way is to add the whole dictionary in one go, Nested_dict[dict] = { 'key': 'value'}.
In this article, we will see how to get data from ImmutableMultiDict in the flask. It is a type of Dictionary in which a single key can have different values. It is used because some elements have multiple values for the same key and it saves the multiple values of a key in form of a list.
You can create a nested dictionary in Python by placing comma-separated dictionaries within curly braces {}. A Python nested dictionary allows you to store and access data using the key-value mapping structure within an existing dictionary.
for example like this:
from werkzeug.datastructures import ImmutableMultiDict
imd = ImmutableMultiDict([('address', u'US'), ('address', 'US'), ('address', 'UK')])
print imd.getlist('address')
prints:
[u'US', 'US', 'UK']
edit:
Your POST-request is sent application/x-www-form-urlencoded
, which means as combination as key/value pairs. It doesn't support a nested dict structure directly. When i try your curl-request I get this:
ImmutableMultiDict([('address[US]', u'San Francisco'), ('address[US]', u'New York'), ('address[UK]', u'London')])
so the keys are interpreted literally here.
and using urllib2
i get this result:
>>> print urllib2.urlopen("http://localhost:5000/post", data=urllib.urlencode(address_dict)).read()
ImmutableMultiDict([('address', u"{'UK': 'London', 'US': 'New York'}")])
here urlencode
simply sends a string representation of the inner dict.
and finally using requests
:
>>> print requests.post("http://localhost:5000/post", data=address_dict).content
ImmutableMultiDict([('address', u'UK'), ('address', u'US')])
here the array is flattened and recurring keys eliminated.
There is simply no defined way how to send a nested dict like yours in an urlencoded way, so you'll have to find another solution.
You gave this example:
curl http://example.com -d "address[US]=San Francisco" -d "address[US]=New York" -d address[UK]=London"
With such a request, request.form
would look like this:
>>> request.form
ImmutableMultiDict([('address[US]', u'San Francisco'), ('address[US]', u'New York'),
('address[UK]', u'London')])
>>> request.form['address[US]']
u'San Francisco'
>>> request.form.getlist('address[US]')
[u'San Francisco', u'New York']
The [
and ]
characters are not special, they are just part of the keys. As documented, a MultiDict will map each key not just to one value but to a list of values. Normal dictionnary access with [] will only give you the first key, but the getlist() method gives the whole list for one key. Other methods are available, see the docs.
If you really want a nested structure, try JSON instead of flat form data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With