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 libraryaiohttp.FormData
: creates a FormData objectform_data.add_field('name', 'value')
: adds a field to the FormData objectsession.post('http://example.com', data=form_data)
: sends the FormData object to the serverresp.status
: prints the response status code
Helpful links
Related
- How to create a websocket server using Python Aiohttp?
- How to create a JSON response using Python Aiohttp?
- How to create a connection pool with Python Aiohttp?
- How to check if a session is closed with Python Aiohttp?
- How to create a server with Python Aiohttp?
- How to reuse a session with Python Aiohttp?
- How to download large files with Python Aiohttp?
- How to redirect with Python Aiohttp?
- How to use Gzip with Python Aiohttp?
More of Python Aiohttp
- How to create a websocket server using Python Aiohttp?
- How to check if a session is closed with Python Aiohttp?
- How to redirect with Python Aiohttp?
- How to get response text with Python Aiohttp?
- How to reuse a session with Python Aiohttp?
- How to retry a request with Python Aiohttp?
- How to get a response with Python Aiohttp?
- How to close a Python Aiohttp session?
- How to rate limit with Python Aiohttp?
See more codes...