Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass array to form data (multipart/form-data) #903

Closed
lukasz-madon opened this issue Jun 3, 2016 · 2 comments · Fixed by #916
Closed

How to pass array to form data (multipart/form-data) #903

lukasz-madon opened this issue Jun 3, 2016 · 2 comments · Fixed by #916
Labels

Comments

@lukasz-madon
Copy link
Contributor

lukasz-madon commented Jun 3, 2016

I need to call API using multipart/form-data with list of strings and list of objects

data = aiohttp.FormData()
data.add_field("emails", candidate["email"], content_type="multipart/form-data")
data.add_field("phones[]", [{"value": phone}], content_type="multipart/form-data")

If I'm not mistaken FormData is designed to support urlencoded params or bytes for files only. Passing anything other than bytes results in ClientRequestError: Can not write request body. Is there a better way that passing everything as files?

What is more the

data.add_field("emails[]", bytes(candidate["email"], "ascii"), content_type="multipart/form-data")

results in emails%5B%5D and form data doesn't allow bytes as names

        elif isinstance(value, (bytes, bytearray, memoryview)):
            if filename is None and content_transfer_encoding is None:
                filename = name

        type_options = multidict.MultiDict({'name': name})
        if filename is not None and not isinstance(filename, str):
            raise TypeError('filename must be an instance of str. '
                            'Got: %s' % filename)
@asvetlov
Copy link
Member

asvetlov commented Jun 3, 2016

Yes, field names and values are intended to be a text.
I really have no idea how to encode data like [{"value": phone}]. IFAIK there is no any standard for cases like this.

lukasz-madon added a commit to rolepoint/aiohttp that referenced this issue Jun 6, 2016
RFC stanard requires all fields to be US-ASCII. Additional `quote`
may provide extra security (RFC-1806 p. 2.3), but does not allow
for interoperability with API that requires ASCII characters outside
`quote` set e.g. emails[] becomes emails%5B%5D

fixes aio-libs#903
@lock
Copy link

lock bot commented Oct 29, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants