My task is to get a json string from request body in utf-8 and encode it to win1251, so i could save it the db in win1251.
data, err := io.ReadAll(c.Request.GetBody())
if err != nil {
panic(err)
}
enc := charmap.Windows1251.NewEncoder()
win, err := enc.Bytes(data)
if err != nil {
panic(err)
}
_, name, _ := charset.DetermineEncoding(win, "application/json")
revel.AppLog.Debug(name)
I tried this, but i'm getting win1252(( Any ideas?
Do not use
charset.DetermineEncoding, you know encoding for your bytes without it.There is no magic function that correctly detects encoding for any data.
charset.DetermineEncodingis not an exception. It detects a few obvious cases and then fallback to "windows-1252":https://cs.opensource.google/go/x/net/+/1185a901:html/charset/charset.go;l=52