简体   繁体   中英

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

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");
}

邮递员截图

Edit:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM