Sending a request in ASP.NET Core API 2.1 with a header with non-ASCII characters

Viewed 1975

I have a very simple ASP.NET Core API. I'm sending headers to this service and it works well.

But the problem is that when I send a character such like "á" in a request header, the request fails with 400: Bad request.

I know this is not a problem with the client—I tested the same code against a Java API and it worked. I also reproduced the same problem with Postman.

How can I tell the server to accept UTF-8 in the request headers?

[HttpPut("/api/Test")]
public IActionResult test([FromHeader(Name = "Word")] String word)
{
  return Ok("I accepted your request");
}

Screenshot of Postman

Edit:

It worked with 3.1, so now we know this is 2.1 related.

1 Answers

It is a limitation of .net core 2.1 and it has been fixed with .net core 2.2.

There is more information about this behavior in this github issue Configure header parsing to allow non-compliant headers

I can see the following solution to fix this issue :

  • migrate to .net core 2.2 or above
  • Url encode header from the client side
  • Use a reverse proxy to encode header before it hits your ASP.net application
Related