How to encode utf-8 request body to windows 1251 in go

1.1k views Asked by At

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?

1

There are 1 answers

1
Dmitry Harnitski On

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.DetermineEncoding is 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