Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a base_url option to ClientSession like httpx does #6013

Closed
1 task done
dcordb opened this issue Sep 19, 2021 · 0 comments · Fixed by #6129
Closed
1 task done

Add a base_url option to ClientSession like httpx does #6013

dcordb opened this issue Sep 19, 2021 · 0 comments · Fixed by #6129

Comments

@dcordb
Copy link

dcordb commented Sep 19, 2021

Is your feature request related to a problem?

Yes, I would like to have the option to declare a base_url to a ClientSession, such that every request use the same base_url for that session.

Instead of writing:

async with aiohttp.ClientSession() as session:
    async with session.get('http://python.org') as response:
        print(await response.text())
    async with session.get('http://python.org/foobar') as response:
        print(await response.text())
   async with session.get('http://python.org/foo/foo/bar') as response:
        print(await response.text())

Write:

async with aiohttp.ClientSession(base_url='http://python.org') as session:
    async with session.get('/') as response:
        print(await response.text())
    async with session.get('/foobar') as response:
        print(await response.text())
   async with session.get('/foo/foo/bar') as response:
        print(await response.text())

Describe the solution you'd like

Add an base_url parameter to ClientSession, like httpx does.

Describe alternatives you've considered

An alternative is having a constant BASE = 'http://python.org' and then say:

async with session.get(f'{BASE}/foobar') as response:
        print(await response.text())

Related component

Client

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
@derlih derlih linked a pull request Oct 24, 2021 that will close this issue
5 tasks
@derlih derlih removed a link to a pull request Oct 24, 2021
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant