question

53586750 avatar image
0 Votes"
53586750 asked TomWilliam834-0336 Suspended answered

Cyrillic symbols in custom headers

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

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.