I'm building a python endpoint on flask (which I'm pretty new to). I'm sending requests from PHP to it (as this is what the majority of our users will be using to send requests). I have a non-associative array of data that should be sent (i.e. something like 'mode' => array('foo', 'bar', 'baz')
)
However in flask when doing str(request.form)
, all I am getting out is ImmutableMultiDict([('mode[1]', 'foo'), ('mode[0]', 'bar'), ('mode[2]', 'baz')])
Now from having looked around on stackoverflow (e.g. Sending array data with jquery and flask) I found this which suggested that I should be able to do something like request.form.getlist('mode[]')
(with or without the square parenthesis - depending on which question you look at on here). However neither work.
Eventually I have found a temporary solution that if you json_encode the data before you put it into the curl post fields (i.e. you do 'mode' => json_encode(array('foo', 'bar', 'baz'))
then things seem to be recieved nicely by Flask (ImmutableMultiDict([('mode', '["foo","bar","baz"]')])
.
But I don't really understand why this solution works - I came across it by good luck rather than any sort of educated guess. Can anyone please explain the logic of why this works or if there is a better (TM) solution.
Thanks in advance!