{"record":{"id":"e5535e508b63bdab","repo":"restsharp/RestSharp","slug":"the-specified-value-is-not-a-valid-host-header-str","errorCode":null,"errorMessage":"The specified value is not a valid Host header string.","messagePattern":"The specified value is not a valid Host header string\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/RestSharp/Parameters/HeaderParameter.cs","lineNumber":69,"sourceCode":"\n    static bool IsInvalidHeaderString(string stringValue) {\n        // ReSharper disable once ForCanBeConvertedToForeach\n        for (var i = 0; i < stringValue.Length; i++) {\n            switch (stringValue[i]) {\n                case '\\r':\n                case '\\n':\n                    return true;\n            }\n        }\n\n        return false;\n    }\n\n    static readonly Regex PortSplitRegex = PartSplit();\n\n    static void CheckAndThrowsForInvalidHost(string name, string value) {\n        if (name == KnownHeaders.Host && InvalidHost(value))\n            throw new ArgumentException(\"The specified value is not a valid Host header string.\", nameof(value));\n\n        return;\n\n        static bool InvalidHost(string host) => Uri.CheckHostName(PortSplitRegex.Split(host)[0]) == UriHostNameType.Unknown;\n    }\n\n#if NET7_0_OR_GREATER\n    [GeneratedRegex(@\":\\d+\")]\n    private static partial Regex PartSplit();\n#else\n    static Regex PartSplit() => new(@\":\\d+\");\n#endif\n}","sourceCodeStart":51,"sourceCodeEnd":82,"githubUrl":"https://github.com/restsharp/RestSharp/blob/6a5082169257438cd085f822f050d93256a8e499/src/RestSharp/Parameters/HeaderParameter.cs#L51-L82","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nrequest.AddHeader(\"Host\", \"https://api.example.com/path\");\n\n// after\nvar host = new Uri(\"https://api.example.com\").Host;\nrequest.AddHeader(\"Host\", host);","handlingStrategy":"validation","validationCode":"if (name == KnownHeaders.Host && Uri.CheckHostName(value.Split(':')[0]) == UriHostNameType.Unknown) throw new ArgumentException(\"Invalid Host header\", nameof(value));","typeGuard":"static bool IsValidHost(string host) => Uri.CheckHostName(host.Split(':')[0]) != UriHostNameType.Unknown;","tryCatchPattern":"try { request.AddHeader(\"Host\", value); } catch (ArgumentException ex) when (ex.Message.Contains(\"Host header string\")) { /* pass only hostname:port, not a full URL */ }","preventionTips":["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."],"tags":["header","host","security","request"],"backgroundTag":null,"analyzedSha":"6a5082169257438cd085f822f050d93256a8e499","analyzedAt":"2026-08-13T20:38:25.807Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}