How to POST multiple FILES using Flask test client? How to POST multiple FILES using Flask test client? flask flask

How to POST multiple FILES using Flask test client?


You send the files as the parameter named file, so you can't look them up with the name file[]. If you want to get all the files named file as a list, you should use this:

upload_files = request.files.getlist("file")

On the other hand, if you really want to read them from file[], then you need to send them like that:

scc.data['file[]'] = # ...

(The file[] syntax is from PHP and it's used only on the client side. When you send the parameters named like that to the server, you still access them using $_FILES['file'].)


Lukas already addressed this,just providing these info as it may help someone

Werkzeug client is doing some clever stuff by storing requests data in MultiDict

@native_itermethods(['keys', 'values', 'items', 'lists', 'listvalues'])class MultiDict(TypeConversionDict):    """A :class:`MultiDict` is a dictionary subclass customized to deal with    multiple values for the same key which is for example used by the parsing    functions in the wrappers.  This is necessary because some HTML form    elements pass multiple values for the same key.    :class:`MultiDict` implements all standard dictionary methods.    Internally, it saves all values for a key as a list, but the standard dict    access methods will only return the first value for a key. If you want to    gain access to the other values, too, you have to use the `list` methods as    explained below.

getList call looks for a given key in the "requests" dictionary. If the key doesn't exist, it returns empty list.

def getlist(self, key, type=None):    """Return the list of items for a given key. If that key is not in the    `MultiDict`, the return value will be an empty list.  Just as `get`    `getlist` accepts a `type` parameter.  All items will be converted    with the callable defined there.    :param key: The key to be looked up.    :param type: A callable that is used to cast the value in the                 :class:`MultiDict`.  If a :exc:`ValueError` is raised                 by this callable the value will be removed from the list.    :return: a :class:`list` of all the values for the key.    """    try:        rv = dict.__getitem__(self, key)    except KeyError:        return []    if type is None:        return list(rv)    result = []    for item in rv:        try:            result.append(type(item))        except ValueError:            pass    return result