Skip to content

Refit requires base address of supplied HttpClient to not end with a slash #1358

Closed

Description

@antoinebj

TL;DR:

It is unnecessarily difficult to have the same HttpClient configuration for manual use and for use by Refit.
Refit requires the HttpClient base address to have no trailing slash, while manual use requires that there be a slash.

Am I missing something? Is that something that could be "fixed"?

--

Original description:

I have a project where my HTTP clients are configured through the mechanisms provided by HttpClientFactory. They get policies and handlers assigned to them and I want to use that configuration everywhere.
For most endpoints, I use Refit which gets its HttpClient supplied by the factory, but for some complex scenarios, I have to use the HttpClient directly without Refit.

My base address is in the form of http://example.com/api/. The trailing slash is required for using HttpClient directly, otherwise it will ignore the api segment when forming the complete URL.

Refit seems to not play nice with that. Instead of working in harmony with that behavior, it tries to work around it. I can see in BuildVoidTaskFuncForMethod that it takes the base path from the HttpClient's base address in order to prepend it itself to the "relative path" in RequestBuilderImplementation.BuildRequestFactoryForMethod. In that prepending, it expects that the base path not end with a slash, and has logic to remove the slash in the case that the base path is the root:

var urlTarget = (basePath == "/" ? string.Empty : basePath) + restMethod.RelativePath;

That "relative path" in RestMethodInfo.RelativePath is enforced to start with a slash, despite the fact that by most if not all familiar conventions, a path that starts with a slash is supposed to be absolute.

Now if Refit creates the HttpClient itself, then it will remove the trailing slash from the base address that it supplies to the HttpClient, so that when it is used, it will forego the base path which has been "manually" prepended to the relative path by Refit.
However if the HttpClient was supplied to Refit, then it has no control over the base address, and if that base address has a trailing slash, then there will be a double slash in the URL: http://example.com/api//my-resource

  1. What is the best way of reconciling the direct use of HttpClient with using Refit on top, when the base address has a path after the domain?

  2. Why wasn't Refit designed to take actual relative paths (not starting with a slash) for the endpoint paths? Those slashes seem to be the source of the conflict in behavior with plain HttpClient. Would it be possible to add a configuration option to switch that behavior?

Activity

  1. changed the title [-]Best practice for base address when HttpClient is provided[/-] [+]Refit requires base address of supplied HttpClient to not end with a slash[/+] on Dec 9, 2022
  2. antoinebj commented on Dec 9, 2022

    @antoinebj
    Author

    I've updated the title and description, hopefully someone will react to this ticket. I currently have to register 2 configurations of HttpClient for the same endpoints because of that.

  3. a-frazier commented on Aug 25, 2023

    @a-frazier

    To add onto this, this behavior violates RFC 3986. From section 5.2.3:

    5.2.3. Merge Paths
    The pseudocode above refers to a "merge" routine for merging a
    relative-path reference with the path of the base URI. This is
    accomplished as follows:
    o If the base URI has a defined authority component and an empty
    path, then return a string consisting of "/" concatenated with the
    reference's path; otherwise,
    o return a string consisting of the reference's path component
    appended to all but the last segment of the base URI's path (i.e.,
    excluding any characters after the right-most "/" in the base URI
    path, or excluding the entire base URI path if it does not contain
    any "/" characters).

    If documentation for C#'s Uri constructor says (and does) the same thing.

    It'd be great if Refit was RFC compliant. It's causing me similar issues to the OP in my work.

  4. glennawatson commented on Jun 16, 2026

    @glennawatson
    Contributor

    Confirmed this is still a real bug on current main. Repro: a supplied HttpClient whose BaseAddress has BOTH a path segment AND a trailing slash, e.g. http://foo/api/, produces a doubled slash.

    Verified resolved URIs for [Get("/someendpoint")]:

    Root cause: BuildRequestMessageForMethodAsync passes client.BaseAddress.AbsolutePath as basePath, and BuildRelativePath in RequestBuilderImplementation.cs only collapses basePath when it equals exactly "/" (line ~1144). A non-root path with a trailing slash (/api/) is concatenated with the leading-slash relative path, yielding //. The existing trailing-slash tests only cover the root case (http://foo/), so this slips through.

    Keeping open as a bug.

  5. added
    🟠 priority: medium
    🔗 area: url-routing
    on Jun 16, 2026
  6. added a commit that references this issue on Jun 17, 2026
    7a3489e
  7. glennawatson commented on Jun 22, 2026

    @glennawatson
    Contributor

    Consolidating this into #1803, which is staying open as the tracking issue for an opt-in RFC-3986 / HttpClient-style URL resolution mode. Same root cause: a base address with both a path segment and a trailing slash combined with a leading-slash relative path yields a doubled slash (http://foo/api//endpoint).

    The fix will be opt-in via RefitSettings so current behavior is unchanged. Follow #1803 for progress. Targeting v12.

  8. github-actions commented on Jul 7, 2026

    @github-actions

    This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  9. locked as resolved and limited conversation to collaborators on Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐛 bug🔗 area: url-routing🟠 priority: medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions