0

I have a request with curl -F "file=/tmp/test.txt" that responses 200, but I cant emulate this request in Python. I tried something like this

mp = aiohttp.MultipartWriter()
mp.append(open('/tmp/test.txt', 'rb'))
result = await session.post(url, data=mp)

but it is not working, the server responses 400. Also I tried

mp.append_form([('file', open('/tmp/test.txt'))]) 

it is not working too. What is the right way to emulate curl -F with aiohttp?

P.S. I also tried to use set_content_disposition() to the result of mp.append(), but it returns None instead of Payload, as it is described in the docs here http://docs.aiohttp.org/en/stable/multipart.html#sending-multipart-requests

sanyassh
  • 8,100
  • 13
  • 36
  • 70

1 Answers1

0

Try to add headers:

headers={
    'Content-Length': '{file lendth}'
    'Content-Type': 'multipart/form-data'
}

/////////////////////////////////////////

mp = aiohttp.MultipartWriter()
mp.append(open('/tmp/test.txt', 'rb'))
result = await session.post(url, data=mp, headers=headers)
Yurii Kramarenko
  • 1,025
  • 6
  • 16