Search code examples
pythonmultipartform-datapython-3.5aiohttp

How do I handle form data in aiohttp responses


I'm looking to get the multipart form data and turn it into a dictionary. Easy enough for json, but this seems to be a bit different.

Current code:

app = web.Application()

async def deploy(request):
    # retrieve multipart form data or
    # x-www-form-urlencoded data
    # convert to a dictionary if not already
    text = "Hello"
    return web.Response(text=text)
app.router.add_post('/', deploy)

web.run_app(app)

Solution

  • You can use the request.post() method.

    app = web.Application()
    
    async def deploy(request):
        # retrieve multipart form data or
        # x-www-form-urlencoded data
        data = await request.post()
        print(data)
        text = "Hello"
        return web.Response(text=text)
    
    app.router.add_post('/', deploy)
    
    web.run_app(app)