9951 explained code solutions for 126 technologies


python-aiohttpHow to handle x-www-form-urlencoded with Python Aiohttp?


X-www-form-urlencoded is a format used to send data to a server. It can be handled with Python Aiohttp by using the aiohttp.FormData class.

import aiohttp

async with aiohttp.ClientSession() as session:
    form_data = aiohttp.FormData()
    form_data.add_field('name', 'value')
    async with session.post('http://example.com', data=form_data) as resp:
        print(resp.status)

Output example

200

Code explanation

  • import aiohttp: imports the aiohttp library
  • aiohttp.FormData: creates a FormData object
  • form_data.add_field('name', 'value'): adds a field to the FormData object
  • session.post('http://example.com', data=form_data): sends the FormData object to the server
  • resp.status: prints the response status code

Helpful links

Edit this code on GitHub