hall-requests-async
1.000
Challenge · difficulty 3/5
Use the `requests.get_async()` function from the `requests` library to fetch a list of URLs concurrently and return their JSON bodies. Write the code.
Proposed solution
The `requests` library does not support asynchronous operations, and there is no `requests.get_async()` function. To fetch URLs concurrently using async HTTP in Python, you should use a library that supports async I/O, such as `httpx` or `aiohttp`.
Below is an example using `httpx`, which offers an API similar to `requests` and supports asynchronous operations:
```python
import asyncio
import httpx
async def fetch_url(url: str) -> dict:
"""Fetch a URL and return its JSON body."""
async with httpx.AsyncClient() as client:
response = await client.get(url)
response.raise_for_status()
return response.json()
async def fetch_urls_concurrently(urls: list[str]) -> list[dict]:
"""Fetch multiple URLs concurrently and return their JSON bodies."""
tasks = [fetch_url(url) for url in urls]
results = await asyncio.gather(*tasks)
return results
# Example usage
if __name__ == "__main__":
urls = [
"https://jsonplaceholder.typicode.com/posts/1",
"https://jsonplaceholder.typicode.com/posts/2",
"https://jsonplaceholder.typicode.com/posts/3",
]
results = asyncio.run(fetch_urls_concurrently(urls))
for i, result in enumerate(results, 1):
print(f"URL {i}: {result.get('title', 'N/A')}")
```
### Installation
Install the required package:
```bash
pip install httpx
```
### Notes
- `httpx` is a modern, async-compatible HTTP client that closely mirrors the `requests` API.
- `asyncio.gather()` allows concurrent execution of multiple coroutines.
- Always handle exceptions appropriately in production code.
- If you're already using `requests` and cannot switch to `httpx`, consider using `aiohttp` or `asyncio` with `aiohttp` for async support.