restsharp/RestSharp · error · ArgumentException
The specified value is not a valid Host header string.
Error message
The specified value is not a valid Host header string.
What it means
Thrown by HeaderParameter.CheckAndThrowsForInvalidHost when the header name is 'Host' and the value fails Uri.CheckHostName validation (after splitting off an optional :port). Ensures only valid hostnames are used for the Host header to prevent malformed requests.
Source
Thrown at src/RestSharp/Parameters/HeaderParameter.cs:69
static bool IsInvalidHeaderString(string stringValue) {
// ReSharper disable once ForCanBeConvertedToForeach
for (var i = 0; i < stringValue.Length; i++) {
switch (stringValue[i]) {
case '\r':
case '\n':
return true;
}
}
return false;
}
static readonly Regex PortSplitRegex = PartSplit();
static void CheckAndThrowsForInvalidHost(string name, string value) {
if (name == KnownHeaders.Host && InvalidHost(value))
throw new ArgumentException("The specified value is not a valid Host header string.", nameof(value));
return;
static bool InvalidHost(string host) => Uri.CheckHostName(PortSplitRegex.Split(host)[0]) == UriHostNameType.Unknown;
}
#if NET7_0_OR_GREATER
[GeneratedRegex(@":\d+")]
private static partial Regex PartSplit();
#else
static Regex PartSplit() => new(@":\d+");
#endif
}View on GitHub (pinned to 6a50821692)
Solutions
- Pass only the hostname (and optional :port) as the Host header value, not a full URL.
- Extract the host via new Uri(url).Host before setting the header.
- Validate the host string with Uri.CheckHostName before adding it.
Example fix
// before
request.AddHeader("Host", "https://api.example.com/path");
// after
var host = new Uri("https://api.example.com").Host;
request.AddHeader("Host", host); Defensive patterns
Strategy: validation
Validate before calling
if (name == KnownHeaders.Host && Uri.CheckHostName(value.Split(':')[0]) == UriHostNameType.Unknown) throw new ArgumentException("Invalid Host header", nameof(value)); Type guard
static bool IsValidHost(string host) => Uri.CheckHostName(host.Split(':')[0]) != UriHostNameType.Unknown; Try / catch
try { request.AddHeader("Host", value); } catch (ArgumentException ex) when (ex.Message.Contains("Host header string")) { /* pass only hostname:port, not a full URL */ } Prevention
- Pass only the hostname (optionally with :port) as the Host header value.
- Derive the host via new Uri(url).Host rather than copying a full URL.
- Validate with Uri.CheckHostName before setting the Host header.
When it happens
Trigger: Constructing a HeaderParameter with name 'Host' (KnownHeaders.Host) or calling request.AddHeader("Host", value) where value is not a valid DNS/IP host, e.g. a URL with scheme, an empty string, or a host with invalid characters.
Common situations: Passing a full URL (https://host) instead of just the hostname; empty or whitespace host value; including path or query in the Host value; copied Host header from a different context.
Related errors
- Invalid character found in header {type}: {value}
- Request resource doesn't contain a valid scheme for an empty
- Binary data format needs a byte array as value
- Cannot set request body using default parameters. Use Reques
- File not found
AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13).
Data as JSON: /api/errors/e5535e508b63bdab.
Report an issue: GitHub.