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

  1. Pass only the hostname (and optional :port) as the Host header value, not a full URL.
  2. Extract the host via new Uri(url).Host before setting the header.
  3. 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

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


AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13). Data as JSON: /api/errors/e5535e508b63bdab. Report an issue: GitHub.