Skip to content

Allow URLs with paths as base_url in ClientSession #6647

Closed
@thewhaleking

Description

Is your feature request related to a problem?

Many sites have an API path in the URL. For example, Jira uses {jira_url}/rest/api/2/ as the base for its API requests.
The current implementation of the ClientSession only allows absolute URLs without a path. Thus, to make a number of Jira requests, you would do something like:

async with aiohttp.ClientSession(base_url="https://jira.com") as client:
    baz = await client.get("/rest/api/2/issue/ISSUE-1")
    bom = await client.get("/rest/api/2/project/ISSUE")

instead of the simpler

async with aiohttp.ClientSession(base_url="https://jira.com/rest/api/2") as client:
    baz = await client.get("/issue/ISSUE-1")
    bom = await client.get("/project/ISSUE")

This somewhat defeats the purpose of having the base_url in the first place.

Describe the solution you'd like

Allow using paths in the base_url, similar to how Requests and HTTPX do.

Describe alternatives you've considered

Just continue typing in the path or using variables with f-strings.

Related component

Client

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct

Metadata

Assignees

No one assigned

    Labels

    HacktoberfestWe think it's good for https://hacktoberfest.digitalocean.com/enhancementgood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions