Skip to content

Latest commit

 

History

History
42 lines (27 loc) · 2.02 KB

File metadata and controls

42 lines (27 loc) · 2.02 KB
title HCR087 — BaseAddress should end with a trailing slash
description A BaseAddress whose path does not end with '/' causes relative request URIs to resolve against the parent segment, silently dropping the last path component.

HCR087

HttpClient.BaseAddress with a non-root path must end with /.

Why

Relative request URIs resolve against BaseAddress using RFC 3986 rules: the last path segment of the base is treated as a resource name and replaced. BaseAddress = "https://api.example.com/v1" combined with GetAsync("users") produces https://api.example.com/users, not https://api.example.com/v1/users — the v1 segment is silently dropped and every relative request hits the wrong endpoint.

Bad

client.BaseAddress = new Uri("https://api.example.com/v1");
var response = await client.GetAsync("users"); // GET https://api.example.com/users

Better

Terminate the base path with /:

client.BaseAddress = new Uri("https://api.example.com/v1/");
var response = await client.GetAsync("users"); // GET https://api.example.com/v1/users

Current Detection

The implementation reports BaseAddress assignments on a System.Net.Http.HttpClient receiver when the value is a constant absolute URI (a string literal or new Uri("literal")) whose path is non-root and does not end with /. Root-only URIs (https://api.example.com), URIs already ending in /, non-constant values, and custom BaseAddress lookalikes are skipped.

Suppression

Suppress only when the trailing-segment drop is intended — for example when every request URI is absolute or begins with /. Prefer adding the slash; it is almost always the intended shape.

References