python-aiohttpHow to get the response body of an aiohttp request in Python?
To get the response body of an aiohttp request in Python, you can use the read()
method of the response object. The following example code will print the response body of a GET request to a given URL:
import aiohttp
async with aiohttp.ClientSession() as session:
async with session.get('https://example.com') as response:
response_body = await response.read()
print(response_body)
Output example
b'<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta charset="utf-8" />\n <meta http-equiv="Content-type" content="text/html; charset=utf-8" />\n <meta name="viewport" content="width=device-width, initial-scale=1" />\n <style type="text/css">\n body {\n background-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;\n \n }\n div {\n width: 600px;\n margin: 5em auto;\n padding: 50px;\n background-color: #fdfdff;\n border-radius: 1em;\n }\n a:link, a:visited {\n color: #38488f;\n text-decoration: none;\n }\n @media (max-width: 700px) {\n body {\n background-color: #fff;\n }\n div {\n width: auto;\n margin: 0 auto;\n border-radius: 0;\n padding: 1em;\n }\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>Example Domain</h1>\n <p>This domain is for use in illustrative examples in documents. You may use this\n domain in literature without prior coordination or asking for permission.</p>\n <p><a href="https://www.iana.org/domains/example">More information...</a></p>\n</div>\n</body>\n</html>\n'
Code explanation
import aiohttp
: imports the aiohttp libraryasync with aiohttp.ClientSession() as session
: creates a client session objectasync with session.get('https://example.com') as response
: sends a GET request to the given URL and stores the response in theresponse
variableresponse_body = await response.read()
: reads the response body and stores it in theresponse_body
variableprint(response_body)
: prints the response body
Helpful links
Related
- How to handle x-www-form-urlencoded with Python Aiohttp?
- How to create a websocket server using Python Aiohttp?
- How to create a JSON response using Python Aiohttp?
- How to disable SSL verification in 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 close a Python Aiohttp session?
- How to rate limit with Python Aiohttp?
More of Python Aiohttp
- How to handle x-www-form-urlencoded with Python Aiohttp?
- How to create a websocket server using Python Aiohttp?
- How to redirect with Python Aiohttp?
- How to reuse a session with Python Aiohttp?
- How to use HTTP2 with Python Aiohttp?
- How to set headers in Python Aiohttp?
- How to rate limit with Python Aiohttp?
- How to create a JSON response using Python Aiohttp?
- How to create a server with Python Aiohttp?
- How to retry a request with Python Aiohttp?
See more codes...