image 5 722x400 - Solved: .NET Core Doesn't Pass Non ASCII Characters In Response Headers

I’m coding a new project in .NET Core Web API. (Dotnet Version: 2.2.401)

I decided that If there is something wrong in the Web API, I can show them (the visitors) “What happened in Web API” in response body. But first, I have to set up global exception things in my project.

Ok.. I did it! I gave them a message in response body.

In sometimes this is not good way If you creating a web api project. You can add to response headers. (I think you must do it)

So, I tried to add same message in response headers and It happened.. Crash..

Because, my error message was containing non ascii characters. (Thanks stackflowover.com)

My message was “Login olamıyor mk.”

image - Solved: .NET Core Doesn't Pass Non ASCII Characters In Response Headers
[Code Snippet from Web API]
image 1 - Solved: .NET Core Doesn't Pass Non ASCII Characters In Response Headers
[Middleware for Global Exception- Startup.cs]
image 4 - Solved: .NET Core Doesn't Pass Non ASCII Characters In Response Headers
[Customize Headers for global exception – HttpResponse_AddApplicationError_Extensions.cs]

When I using Postman app (Postman is a tool used to send requests), It seems like there is an issue. Because body and header is empty.

image 2 1024x631 - Solved: .NET Core Doesn't Pass Non ASCII Characters In Response Headers
[Response Body from WEB API – Postman]
image 3 1024x599 - Solved: .NET Core Doesn't Pass Non ASCII Characters In Response Headers
[Response Headers form WEB API – Postman]

Finally, I found the solution. The response body was working, headers not working because of non ascii characters issue. I decided that the message of customize header must be base64 in response header.

It works!

image 5 - Solved: .NET Core Doesn't Pass Non ASCII Characters In Response Headers
[Converting to base64 – Extensions.cs]
image 6 1024x531 - Solved: .NET Core Doesn't Pass Non ASCII Characters In Response Headers
[Response Headers from WEB API – Postman]
image 7 1024x531 - Solved: .NET Core Doesn't Pass Non ASCII Characters In Response Headers
[Response Body from WEB API – Postman]

Ok guys, It’s working and I have to write some code!

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

This site uses Akismet to reduce spam. Learn how your comment data is processed.