-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Accept per-field headers in multipart POST requests (and create multipart/mixed requests) #1736
Comments
Yes there is. Take a look at the changelog. The syntax for doing this and including things that aren't files (like some JSON) is a bit of a pain, but here it is: import json
from cStringIO import StringIO
metadata = {'key1': 'value1', 'key2': 'value2'}
json = StringIO(json.dumps(metadata))
files = {"data": ("", json, "application/json; charset=UTF-8", {}), "file": ("image.jpg", imagefile, "", {})}
r = requests.post('http://www.api.com/, files=files) The reason this API is totally nasty is because the Finally, yes, you can do a from requests import Request, Session
# Insert all that files nonsense here.
s = Session()
req = Request('POST', url, files=files)
prepped = s.prepare_request(req)
prepped.headers['Content-Type'] = 'multipart/mixed'
resp = s.send(prepped) |
Thanks a lot for such a great answer! Yes, the API for this isn't straightforward but at least it's possible without touching I also don't like the current method because it kind of covers the most typical use cases but some more special cases (yet still valid ones) are maybe possible but only with those "hacks". The |
Anything we do to improve our flexibility here should be done outside of |
@Lukasa there was a reason I started https://github.com/sigmavirus24/requests-data-schemes. It was a bad name but the intent was to provide a better way of doing complicated multipart uploads (specifically when no files should be sent, which is an extremely common use case). |
Related: urllib3/urllib3#215 |
Currently there is no way of doing such requests:
Sending a file along with a JSON encoded metadata is a very typical task for API implementations.
What is even is weirder that we can do this for files but not for other fields that are sent with the files.
Also, is there any way to compose a multipart POST request with
multipart/mixed
header? (With keeping the possibility to choose the fields and their headers.)Thanks, Jan
The text was updated successfully, but these errors were encountered: