Skip to content

RequestHeaderEncodingSelector doesn't default to UTF8 #61189

@daniel-liuzzi

Description

@daniel-liuzzi

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

According to the docs, RequestHeaderEncodingSelector should use the default UTF8Encoding when null is return, but this doesn't seem to be the case.

Either returning null or leaving RequestHeaderEncodingSelector unset results in 400 Bad Request, whereas explicitly returning Encoding.UTF8 works as expected.

Expected Behavior

Setting RequestHeaderEncodingSelector shouldn't be necessary to use default UTF8 encoding.

Steps To Reproduce

Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.ConfigureKestrel(
    options =>
    {
        // Replacing null below with System.Text.Encoding.UTF8 works as expected
        options.RequestHeaderEncodingSelector = _ => null;
    });
var app = builder.Build();

app.MapGet("/", (HttpContext context) => context.Request.Headers);
app.Run();

test.http

GET http://localhost:5170/
dummy: Ç

Response

< HTTP/1.1 400 Bad Request
< Content-Length: 0
< Connection: close
< Date: Thu, 27 Mar 2025 15:53:23 GMT
< Server: Kestrel

Exceptions (if any)

No response

.NET Version

9.0.101

Anything else?

As an aside, the following answer in Stack Overflow confirms this behavior:

Eventually we managed to solve this issue via setting the encoding of the Http Headers to UTF-8. I find it quiet interesting, because UTF-8 should be the default value anyways. *We have no idea why it‘s not working without setting it implicitly, but until now we haven’t had any side effects.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Status: Resolvedarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions