Hey,
suppose the server got a big uploaded file from the client, and the server forward the uploaded file to another url with some extra data.
the handler:
async def test(request):
mr = await request.multipart()
post = {}
while True:
part = await mr.next()
if part is None: break
data = {'foo': 'bar', 'file': part.read()}
async with aiohttp.ClientSession() as s:
async with
s.post(another_url, data = data) as r:
return web.json_response(await r.json())
@aiohttp.streamer
def file_sender(writer, file_name=None):
with open(file_name, 'rb') as f:
chunk = f.read(2**16)
while chunk:
yield from writer.write(chunk)
chunk = f.read(2**16)
# Then you can use `file_sender` as a data provider:
async with session.post('http://httpbin.org/post',
data=file_sender(file_name='huge_file')) as resp:
print(await resp.text())
but how can i add some extra fields in the data?