-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[SOLVED] How to send files and data in the same request #3571
Comments
GitMate.io thinks the contributor most likely able to help you is @asvetlov. Possibly related issues are #1651 (request.files in addition to request.data), #1155 (how-to retrieve the data (body) in aiohttp server from requests.get), #1469 (Request.post() stores data in temp files), #3034 (Multipart file form data with name), and #370 (Server closes connection before sending data). |
I guess it's more something like this, but still does not work: async with aiohttp.ClientSession(auth=auth) as session:
with aiohttp.MultipartWriter("mixed") as mpwriter:
with aiohttp.MultipartWriter("related") as subwriter:
subwriter.append(
attachment, {"Content-Type": "multipart/form-data"}
)
mpwriter.append(subwriter)
with aiohttp.MultipartWriter("related") as subwriter:
subwriter.append_json(
mailgun_data, {"Content-Type": "application/json"}
)
mpwriter.append(subwriter)
async with session.post(
f"https://api.mailgun.net/v3/{ API_MAILGUN_DOMAIN }/messages",
data=mpwriter,
) as resp:
response = await resp.text()
print(response) |
Sorry, I'm not familiar with MailGun API. |
Well, your first example is quite correct, but it seems like there is some MailGun API specifics. According their examples, there should be something like:
|
Thank you for the reply, the requests example that works look like: def send_complex_message():
return requests.post(
"https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages",
auth=("api", "YOUR_API_KEY"),
files=[("attachment", ("test.jpg", open("files/test.jpg","rb").read())),
("attachment", ("test.txt", open("files/test.txt","rb").read()))],
data={"from": "Excited User <YOU@YOUR_DOMAIN_NAME>",
"to": "foo@example.com",
"cc": "baz@example.com",
"bcc": "bar@example.com",
"subject": "Hello",
"text": "Testing some Mailgun awesomness!",
"html": "<html>HTML version of the body</html>"}) if it helps. It still didn't work with the above example but I keep digging. |
Well, I took curl example, dump what it really sends and tried to reimplement the same payload with aiohttp. But honestly, didn't checked the result. But it looks like as sequential, without any nesting. |
Can we close it? |
yes |
Long story short
I want to send files (binary data) and json data in the same request but failing to grasp the documentation or see any examples in github issues.
Expected behaviour
Send file(s) and post data
Actual behaviour
It doesn't seem to include post parameters
Steps to reproduce
output:
without the file the request works.
Your environment
I use aiohttp client, on arch linux 4.20.3 aiohttp version: 3.4.4
The text was updated successfully, but these errors were encountered: