dotnet/yarp · error · ArgumentException

'headerName' cannot be null or empty.

Error message

'headerName' cannot be null or empty.

What it means

ResponseTransform.TakeHeader is a static helper that extracts and removes a response header value from the HttpContext.Response (or the proxy response if headers have not been copied yet). It throws ArgumentException when headerName is null or empty, because it cannot locate a header without a name. Custom response transforms calling this helper with an empty name hit it.

Source

Thrown at src/ReverseProxy/Transforms/ResponseTransform.cs:38

    /// </summary>
    public abstract ValueTask ApplyAsync(ResponseTransformContext context);

    /// <summary>
    /// Removes and returns the current header value by first checking the HttpResponse
    /// and falling back to the value from HttpResponseMessage or HttpContent only if
    /// <see cref="ResponseTransformContext.HeadersCopied"/> is not set.
    /// This ordering allows multiple transforms to mutate the same header.
    /// </summary>
    /// <param name="context">The transform context.</param>
    /// <param name="headerName">The name of the header to take.</param>
    /// <returns>The response header value, or StringValues.Empty if none.</returns>
    public static StringValues TakeHeader(ResponseTransformContext context, string headerName)
    {
        ArgumentNullException.ThrowIfNull(context);

        if (string.IsNullOrEmpty(headerName))
        {
            throw new ArgumentException($"'{nameof(headerName)}' cannot be null or empty.", nameof(headerName));
        }

        if (context.HttpContext.Response.Headers.TryGetValue(headerName, out var existingValues))
        {
            context.HttpContext.Response.Headers.Remove(headerName);
        }
        else if (context.ProxyResponse is { } proxyResponse && !context.HeadersCopied)
        {
            if (!RequestUtilities.TryGetValues(proxyResponse.Headers, headerName, out existingValues))
            {
                RequestUtilities.TryGetValues(proxyResponse.Content.Headers, headerName, out existingValues);
            }
        }

        return existingValues;
    }

    /// <summary>

View on GitHub (pinned to bd11867bee)

Solutions

  1. Pass a concrete header name to TakeHeader.
  2. Skip empty entries when iterating header-name collections.
  3. Guard with a null/empty check before calling TakeHeader.

Example fix

// before
var val = ResponseTransform.TakeHeader(context, headerName /* possibly empty */);
// after
if (!string.IsNullOrEmpty(headerName))
{
    var val = ResponseTransform.TakeHeader(context, headerName);
}
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(headerName))
{
    // skip or log; do not call TakeHeader with an empty name
    return;
}
var value = ResponseTransform.TakeHeader(context, headerName);

Type guard

static bool IsValidHeaderName(string? name)
    => !string.IsNullOrWhiteSpace(name);

Prevention

When it happens

Trigger: A custom ResponseTransform implementation calling `ResponseTransform.TakeHeader(context, headerName)` where headerName is null or empty. This is a developer-API path for custom transform authors.

Common situations: A custom transform iterating over header names where one is empty; headerName sourced from a route value or config that resolved to empty; copy-pasted transform stub.

Related errors


AI-assisted analysis of dotnet/yarp@bd11867bee (2026-08-13). Data as JSON: /api/errors/926b1bc467535eeb. Report an issue: GitHub.