Reputation: 69
I'm trying to make a simple service, which can download a file from a post request and then redirect it to another service. How can I pass my file through?
I tried using HTTPFound, but I don't know how to pass the file through:
raise web.HTTPFound(
location='some_url_to_redirect',
headers=request.headers,
body=request.content,
)
from aiohttp import web, MultipartReader
async def store_files_handler(request):
reader = MultipartReader.from_response(request)
field = await reader.next()
file_name = field.filename
file_path = f"{FOLDER}/file_name"
with open(file_path, 'wb') as file:
while True:
chunk = await field.read_chunk()
if not chunk:
break
file.write(chunk)
raise web.HTTPFound(
location='some_url_to_redirect',
headers=request.headers,
body=reader,
)
def register(app):
app.add_routes([web.post('/store', store_files_handler)])
Upvotes: 2
Views: 2017
Reputation: 69
Actually, i decided to use, code 307, so it will be:
web.HTTPTemporaryRedirect('some_url_to_redirect')
it can redirect POST request
Upvotes: 2
Reputation: 11091
You can't really send content with a 302 redirect response (most browsers will just ignore it and redirect). The target location must serve the file. So you need to extend your router with that location and serve the file there.
app.add_routes([web.post('/store', store_files_handler)])
app.add_routes([web.static('/location_to_file', 'path_to_static_folder')])
Or you could serve it directly from /store
endpoint, whichever fits your design
Upvotes: 0