Repository navigation
Description
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
-
What is the best way of reconciling the direct use of
HttpClientwith using Refit on top, when the base address has a path after the domain? -
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
[-]Best practice for base address when HttpClient is provided[/-][+]Refit requires base address of supplied HttpClient to not end with a slash[/+]antoinebj commentedon Dec 9, 2022
I've updated the title and description, hopefully someone will react to this ticket. I currently have to register 2 configurations of
HttpClientfor the same endpoints because of that.a-frazier commentedon Aug 25, 2023
To add onto this, this behavior violates RFC 3986. From section 5.2.3:
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.
glennawatson commentedon Jun 16, 2026
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.
fix: correct URL and query string building edge cases (reactiveui#2137)
glennawatson commentedon Jun 22, 2026
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.
github-actions commentedon Jul 7, 2026
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.