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

Multipart file form data with name #3034

Closed
MattIPv4 opened this issue May 28, 2018 · 9 comments
Closed

Multipart file form data with name #3034

MattIPv4 opened this issue May 28, 2018 · 9 comments
Labels

Comments

@MattIPv4
Copy link

MattIPv4 commented May 28, 2018

Long story short

So I want to post a file as multipart data to a file uploading site.
The request payload needs to have the name 'files[]'.
I frankly have no idea how to do this and the documentation really isn't helping.

Current code

# Generate multipart
mpwriter = MultipartWriter('form-data')
mpwriter.append(open(path, 'rb'), {'name': 'files[]', 'filename': os.path.split(path)[1]})

# Go, go, go
headers = {'Accept':'application/json'}
async with self.bot.session.post("https://rokket.space/upload",
                                                      data=mpwriter, headers=headers) as resp:
    print((await resp.text()))

This gives me a success from the uploader but the uploader never detects the file, which suggests the multipart file doesn't actually get given the name files[] as it should.

Valid request example

This is an example of a valid request done through their site.
Headers: https://i.dabbitpri.me/azrr4yk7on.png
The beginning of the multipart file payload, containing the all important name and filename: https://i.dabbitpri.me/x2s2xie8dw.png

Requests

This code, using requests, works perfectly: https://hastebin.com/ecemitilat.py

@eLvErDe
Copy link

eLvErDe commented May 20, 2019

Excuse me guy but why all ticket related to multipart key name are closed without a solution ?
I'm hitting exactly the same bug.

@asvetlov
Copy link
Member

I guess if the issue creator closed it in the same day when opened -- he found the solution.

@MattIPv4
Copy link
Author

@eLvErDe @asvetlov The issue was actually due to the bug I described in #3035 which people claim is now fixed. I haven't tested this as by the time it was fixed, I had already created a new solution to my issue.

@asvetlov
Copy link
Member

#3035 bugfix is not released yet IIRC

@MattIPv4
Copy link
Author

@asvetlov Following the PR chain, you end at #3479 which was merged before the release of 3.5.4?

@eLvErDe
Copy link

eLvErDe commented May 21, 2019

I ywasn't able either to create a multiform payload with file content attached and a specific name.

I ended up using: https://github.com/aio-libs/aiohttp/blob/master/aiohttp/formdata.py which seems to be undocumented but worked, not sure about the drawbacks...

Regards

@asvetlov
Copy link
Member

FormData is the official way to do it.
Sadly, it was not documented until #3755 and #3786

@eLvErDe
Copy link

eLvErDe commented May 21, 2019

So there's still something wrong because I cannot figure out reading the official documentation. Or maybe, these two PR are not released yet ?

@asvetlov
Copy link
Member

Yes, PRs are not released. It is a new addition

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

No branches or pull requests

3 participants