{"id":13501597,"url":"https://github.com/encode/requests-async","last_synced_at":"2025-03-29T09:30:52.279Z","repository":{"id":57461330,"uuid":"176784301","full_name":"encode/requests-async","owner":"encode","description":"async-await support for `requests`. ✨ 🍰 ✨","archived":true,"fork":false,"pushed_at":"2019-07-22T21:40:29.000Z","size":98,"stargazers_count":934,"open_issues_count":0,"forks_count":36,"subscribers_count":24,"default_branch":"master","last_synced_at":"2025-01-06T12:57:55.350Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/encode.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-20T17:28:58.000Z","updated_at":"2025-01-06T01:32:48.000Z","dependencies_parsed_at":"2022-09-17T08:11:25.869Z","dependency_job_id":null,"html_url":"https://github.com/encode/requests-async","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/encode%2Frequests-async","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/encode%2Frequests-async/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/encode%2Frequests-async/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/encode%2Frequests-async/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/encode","download_url":"https://codeload.github.com/encode/requests-async/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246167027,"owners_count":20734377,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T22:01:43.067Z","updated_at":"2025-03-29T09:30:52.040Z","avatar_url":"https://github.com/encode.png","language":"Python","readme":"\n**This work has been overtaken by the `httpx` project:** https://github.com/encode/httpx\n\nWe now recommend using `httpx.AsyncClient()` for async/await support with a requests-compatible API.\n\n**Note**: Use `ipython` to try this from the console, since it supports `await`.\n\n```python\n\u003e\u003e\u003e import httpx\n\u003e\u003e\u003e client = httpx.AsyncClient()\n\u003e\u003e\u003e r = await client.get('https://www.example.org/')\n\u003e\u003e\u003e r.status_code\n200\n\u003e\u003e\u003e r.text\n'\u003c!doctype html\u003e\\n\u003chtml\u003e\\n\u003chead\u003e\\n\u003ctitle\u003eExample Domain\u003c/title\u003e...'\n```\n\n---\n\n# requests-async\n\nBrings support for `async`/`await` syntax to Python's fabulous `requests` library.\n\n\u003cp\u003e\n\u003ca href=\"https://travis-ci.org/encode/requests-async\"\u003e\n    \u003cimg src=\"https://travis-ci.org/encode/requests-async.svg?branch=master\" alt=\"Build Status\"\u003e\n\u003c/a\u003e\n\u003ca href=\"https://codecov.io/gh/encode/requests-async\"\u003e\n    \u003cimg src=\"https://codecov.io/gh/encode/requests-async/branch/master/graph/badge.svg\" alt=\"Coverage\"\u003e\n\u003c/a\u003e\n\u003ca href=\"https://pypi.org/project/requests-async/\"\u003e\n    \u003cimg src=\"https://badge.fury.io/py/requests-async.svg?cache0\" alt=\"Package version\"\u003e\n\u003c/a\u003e\n\u003c/p\u003e\n\n## Requirements\n\n* Python 3.6+\n\n## Installation\n\n```shell\n$ pip install requests-async\n```\n\n## Usage\n\nJust use *the standard requests API*, but use `await` for making requests.\n\n**Note**: Use `ipython` to try this from the console, since it supports `await`.\n\n```python\nimport requests_async as requests\n\n\nresponse = await requests.get('https://example.org')\nprint(response.status_code)\nprint(response.text)\n```\n\nOr use explicit sessions, with an async context manager.\n\n```python\nimport requests_async as requests\n\n\nasync with requests.Session() as session:\n    response = await session.get('https://example.org')\n    print(response.status_code)\n    print(response.text)\n```\n\nThe `requests_async` package subclasses `requests`, so you're getting all the\nstandard behavior and API you'd expect.\n\n## Streaming responses \u0026 requests\n\nThe `iter_content()` and `iter_lines()` methods are async iterators.\n\n```python\nresponse = await requests.get('https://example.org', stream=True)\nasync for chunk in response.iter_content():\n    ...\n```\n\nThe method signatures remain the same as the standard `requests` API:\n\n* `iter_content(chunk_size=1, decode_unicode=False)`\n* `iter_lines(chunk_size=512, decode_unicode=False, delimiter=None)`\n\nThe methods will yield text if `decode_unicode` is set and the response includes\nan encoding. Otherwise the methods will yield bytes.\n\nYou can also stream request bodies. To do this you should use an asynchronous\ngenerator that yields bytes.\n\n```python\nasync def stream_body():\n    ...\n\nresponse = await requests.post('https://example.org', data=stream_body())\n```\n\n## Mock Requests\n\nIn some situations, such as when you're testing a web application, you may\nnot want to make actual outgoing network requests, but would prefer instead\nto mock out the endpoints.\n\nYou can do this using the `ASGISession`, which allows you to plug into\nany ASGI application, instead of making actual network requests.\n\n```python\nimport requests_async\n\n# Create a mock service, with Starlette, Responder, Quart, FastAPI, Bocadillo,\n# or any other ASGI web framework.\nmock_app = ...\n\nif TESTING:\n    # Issue requests to the mocked application.\n    requests = requests_async.ASGISession(mock_app)\nelse:\n    # Make live network requests.\n    requests = requests_async.Session()\n```\n\n## Test Client\n\nYou can also use `ASGISession` as a test client for any ASGI application.\n\nYou'll probably want to install `pytest` and `pytest-asyncio`, or something\nequivalent, to allow you to write `async` test cases.\n\n```python\nfrom requests_async import ASGISession\nfrom myproject import app\nimport pytest\n\n@pytest.mark.asyncio\nasync def test_homepage():\n    client = ASGISession(app)\n    response = await client.get(\"/\")\n    assert response.status_code == 200\n```\n\n## Alternatives\n\n* The [`httpx` package][httpx] both sync and async HTTP clients, with a requests-compatible API.\n* The [`aiohttp` package][aiohttp] provides an alternative client for making async HTTP requests.\n\n[issues]: https://github.com/encode/requests-async/issues\n[aiohttp]: https://docs.aiohttp.org/en/stable/client.html\n[httpx]: https://github.com/encode/httpx\n","funding_links":[],"categories":["Python","HarmonyOS"],"sub_categories":["Windows Manager"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fencode%2Frequests-async","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fencode%2Frequests-async","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fencode%2Frequests-async/lists"}