Cyrillic symbols in custom headers

Таня Катина 21 Reputation points
2021-04-09T14:32:08.137+00:00

Hi! I'm trying to figure out how to properly pass non-ACSII characters in HTTP headers?
I need to pass Cyrillic characters, but HTTP does not allow me to do this.
Are there any recommendations for this situation in other standards?

I found this discussion - https://social.msdn.microsoft.com/Forums/en-US/7886006f-820e-4c42-91b0-fd94c7c7cfa5/http-headers-in-non-usascii-encoding?forum=netfxnetcom . I would like to understand the prerequisites for url encoding.

Thank you

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,580 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 48,826 Reputation points
    2021-04-09T15:01:57.507+00:00

    The spec says that only ASCII characters are allowed in HTTP headers. Anything else can be ignored and therefore won't work. If you need to store non-ASCII data then encoding is the only way. I don't believe that URL encoding is the correct solution though. URL encoding is used to map characters that have special meaning in a URL (like slash, question marks and ampersands) to something else so the URL can be properly encoded. That isn't an issue for HTTP headers as they are just string values. You can put anything you want in them (as far as ASCII goes).

    The correct solution, to me, is to Base64 encode whatever value you have. This will convert the entire string to a series of hexadecimal values that you can then decode on the other side. This is the primary approach we use to encode things like binary data that needs to be stored as a string. In C# it is easy to do.

    var someStringToEncode = "ABC123";
    string encodedString = Convert.ToBase64String(Encoding.Unicode.GetBytes(someStringToEncode));
    
    //To decode
    string decodedString = Encoding.Unicode.GetString(Convert.FromBase64String(data));
    

    Note that I'm using the Unicode encoding here because it doesn't matter for base64 encoding. But each side of the fence needs to use the same encoding to avoid translation issues.

    0 comments No comments

0 additional answers

Sort by: Most helpful