{"record":{"id":"5dc86319dea0ddc1","repo":"dotnet/yarp","slug":"headername-cannot-be-null-or-empty-5dc863","errorCode":null,"errorMessage":"'headerName' cannot be null or empty.","messagePattern":"'headerName' cannot be null or empty\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/ReverseProxy/Transforms/RequestTransform.cs","lineNumber":34,"sourceCode":"    /// <summary>\n    /// Transforms any of the available fields before building the outgoing request.\n    /// </summary>\n    public abstract ValueTask ApplyAsync(RequestTransformContext context);\n\n    /// <summary>\n    /// Removes and returns the current header value by first checking the HttpRequestMessage,\n    /// then the HttpContent, and falling back to the HttpContext only if\n    /// <see cref=\"RequestTransformContext.HeadersCopied\"/> is not set.\n    /// This ordering allows multiple transforms to mutate the same header.\n    /// </summary>\n    /// <param name=\"context\">The transform context.</param>\n    /// <param name=\"headerName\">The name of the header to take.</param>\n    /// <returns>The requested header value, or StringValues.Empty if none.</returns>\n    public static StringValues TakeHeader(RequestTransformContext context, string headerName)\n    {\n        if (string.IsNullOrEmpty(headerName))\n        {\n            throw new ArgumentException($\"'{nameof(headerName)}' cannot be null or empty.\", nameof(headerName));\n        }\n\n        var proxyRequest = context.ProxyRequest;\n\n        if (RequestUtilities.TryGetValues(proxyRequest.Headers, headerName, out var existingValues))\n        {\n            proxyRequest.Headers.Remove(headerName);\n        }\n        else if (proxyRequest.Content is { } content && RequestUtilities.TryGetValues(content.Headers, headerName, out existingValues))\n        {\n            content.Headers.Remove(headerName);\n        }\n        else if (!context.HeadersCopied)\n        {\n            existingValues = context.HttpContext.Request.Headers[headerName];\n        }\n\n        return existingValues;","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/dotnet/yarp/blob/bd11867bee7df522e7fd3effb08a9c85fd616908/src/ReverseProxy/Transforms/RequestTransform.cs#L16-L52","documentation":"RequestTransform.TakeHeader is a static helper that extracts and removes a header value from the proxy request (or content, or HttpContext). It throws ArgumentException when headerName is null or empty because it cannot look up a header without a name. Custom transforms calling TakeHeader with an empty name hit this.","triggerScenarios":"A custom RequestTransform implementation calling `RequestTransform.TakeHeader(context, headerName)` where headerName is null or empty. This is a developer-API path, not config-driven; it is hit when writing custom transform logic.","commonSituations":"A custom transform that iterates over a list of header names and one entry is empty; a transform that takes headerName from a route value that resolved to empty; copy-paste of a transform stub without filling in the name.","solutions":["Pass a concrete header name to TakeHeader.","Skip empty entries when iterating over header-name collections.","Coalesce null/empty to a default header name if applicable, or skip the transform step."],"exampleFix":"// before\nvar val = RequestTransform.TakeHeader(context, headerName /* may be empty */);\n// after\nif (!string.IsNullOrEmpty(headerName))\n{\n    var val = RequestTransform.TakeHeader(context, headerName);\n}","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(headerName))\n{\n    // skip or log; do not call TakeHeader with an empty name\n    return;\n}\nvar value = RequestTransform.TakeHeader(context, headerName);","typeGuard":"static bool IsValidHeaderName(string? name)\n    => !string.IsNullOrWhiteSpace(name);","tryCatchPattern":null,"preventionTips":["In custom transforms, always guard TakeHeader calls with a null/empty check.","When iterating header-name collections, filter out empty strings first.","Write a unit test that passes an empty header name to your custom transform to verify graceful handling."],"tags":["yarp","transforms","argument-validation","headers","custom-transform"],"backgroundTag":null,"analyzedSha":"bd11867bee7df522e7fd3effb08a9c85fd616908","analyzedAt":"2026-08-13T21:29:49.359Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}