391
votes

Consider the following code, where the BaseAddress defines a partial URI path.

using (var handler = new HttpClientHandler())
using (var client = new HttpClient(handler))
{
    client.BaseAddress = new Uri("http://something.com/api");
    var response = await client.GetAsync("/resource/7");
}

I expect this to perform a GET request to http://something.com/api/resource/7. But it doesn't.

After some searching, I find this question and answer: HttpClient with BaseAddress. The suggestion is to place / on the end of the BaseAddress.

using (var handler = new HttpClientHandler())
using (var client = new HttpClient(handler))
{
    client.BaseAddress = new Uri("http://something.com/api/");
    var response = await client.GetAsync("/resource/7");
}

It still doesn't work. Here's the documentation: HttpClient.BaseAddress What's going on here?

5
Possible duplicate of HttpClient with BaseAddressGeorge Lanetz
@ГеоргийЛанец The reverse duplicate has already been proposed. I wrote this question specifically because that other question was not written in a way that was very discoverable by people with the same problem, and I wrote the answer here because the answer over there left off an important point.Timothy Shields
but this question is asked laterGeorge Lanetz
@ГеоргийЛанец That's not how it works. Usually the most "canonical" question is the one that gets the duplicates pointing to it. That other question was about a single problem that user was having instead of reading like an FAQ.Timothy Shields
@ГеоргийЛанец Also notice I reference that other question in this question, and I explain why the other question and answer are insufficient for solving the problem.Timothy Shields

5 Answers

913
votes

It turns out that, out of the four possible permutations of including or excluding trailing or leading forward slashes on the BaseAddress and the relative URI passed to the GetAsync method -- or whichever other method of HttpClient -- only one permutation works. You must place a slash at the end of the BaseAddress, and you must not place a slash at the beginning of your relative URI, as in the following example.

using (var handler = new HttpClientHandler())
using (var client = new HttpClient(handler))
{
    client.BaseAddress = new Uri("http://something.com/api/");
    var response = await client.GetAsync("resource/7");
}

Even though I answered my own question, I figured I'd contribute the solution here since, again, this unfriendly behavior is undocumented. My colleague and I spent most of the day trying to fix a problem that was ultimately caused by this oddity of HttpClient.

80
votes

Reference Resolution is described by RFC 3986 Uniform Resource Identifier (URI): Generic Syntax. And that is exactly how it supposed to work. To preserve base URI path you need to add slash at the end of the base URI and remove slash at the beginning of relative URI.

If base URI contains non-empty path, merge procedure discards it's last part (after last /). Relevant section:

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:

  • 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

  • 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 relative URI starts with a slash, it is called a absolute-path relative URI. In this case merge procedure ignore all base URI path. For more information check 5.2.2. Transform References section.

1
votes

I also came upon this same issue with BaseAddress. I decided to not use BaseAddress at all and simplest solution would be a simple one-liner addition:

Uri GetUri(string path) => new Uri("http://something.com/api" + path);

Then your code would become:

Uri GetUri(string path) => new Uri("http://something.com/api" + path);
using (var handler = new HttpClientHandler())
using (var client = new HttpClient(handler))
{
    // Remove BaseAddress completely
    // client.BaseAddress = new Uri("http://something.com/api");
    var response = await client.GetAsync(GetUri("/resource/7"));
}

I have not investigated pros and cons of using BaseAddress over this, but for me this works flawlessly. Hope this helps somebody.

1
votes

if you are using httpClient.SendAsync() there is no string overload like the overloads in Get, Post and other verb-specific methods.

But you can create relative Uri with giving UriKind.Relative as second param

        var httpRequestMessage = new HttpRequestMessage
        {
            Method = httpMethod,
            RequestUri = new Uri(relativeRequestUri, UriKind.Relative),
            Content = content
        };

        using var httpClient = HttpClientFactory.CreateClient("XClient");
        var response = await httpClient.SendAsync(httpRequestMessage);
        var responseText = await response.Content.ReadAsStringAsync();
0
votes

Ran into a issue with the HTTPClient, even with the suggestions still could not get it to authenticate. Turns out I needed a trailing '/' in my relative path.

i.e.

var result = await _client.GetStringAsync(_awxUrl + "api/v2/inventories/?name=" + inventoryName);
var result = await _client.PostAsJsonAsync(_awxUrl + "api/v2/job_templates/" + templateId+"/launch/" , new {
                inventory = inventoryId
            });