Converting "=? UTF 8? .." (RFC 2047) to a regular string in golang

I am using an API and returning something similar for text in another language:

=?UTF 8?B?2KfZhNiu2LfZiNin2Kog2KfZhNiq2Yog2KrYrNmF2Lkg2KjZitmG?= =?UTF 8?B?INit2YHYuCDYp9mE2YLYsdin2ZPZhiDYp9mE2YPYsdmK2YUg2YjZgQ==?= =?UTF 8?B?2YfZhdmHINmF2YXYpyDYp9mU2YXZhNin2Ycg2KfZhNi52YTYp9mF?= =?UTF 8?B?2Kkg2LnYqNivINin2YTZhNmHINin2YTYutiv2YrYp9mGLnBkZg==?=

Is this a common format? How could I convert this to a regular string in golang?

Golang usually manages several languages โ€‹โ€‹well, but I'm not sure how to do this.

+5
source share
2 answers

Sometimes your API returns data encoded in RFC 2047 format. This basically defines the following:

 encoded-word = "=?" charset "?" encoding "?" encoded-text "?=" 

This means that your encoding is UTF-8 (very convenient, since it is Go's own character set), and your Base64 encoding. The text you must decode is the number between "B?" and "? =". So all you have to do is take this text and call:

 base64.StdEncoding.DecodeString(text) 

to get the original string of UTF-8.

There is a decodeRFC2047Word() function in the net/mail package Go stdlib that supports B and Q encodings and UTF-8 , US-ASCII and ISO-8859-1 . Unfortunately, it is not exported, but you can extract as much inspiration from it as you need;)

BTW: I just noticed that in your examples the character string is UTF 8 , which is a bit odd, since the official name of the encoding is UTF-8 .

+6
source

Since Go 1.5 you can use mime.WordDecoder.DecodeHeader :

 package main import ( "fmt" "mime" ) func main() { dec := new(mime.WordDecoder) header, err := dec.DecodeHeader("=?UTF-8?B?2KfZhNiu2LfZiNin2Kog2KfZhNiq2Yog2KrYrNmF2Lkg2KjZitmG?= =?UTF-8?B?INit2YHYuCDYp9mE2YLYsdin2ZPZhiDYp9mE2YPYsdmK2YUg2YjZgQ==?= =?UTF-8?B?2YfZhdmHINmF2YXYpyDYp9mU2YXZhNin2Ycg2KfZhNi52YTYp9mF?= =?UTF-8?B?2Kkg2LnYqNivINin2YTZhNmHINin2YTYutiv2YrYp9mGLnBkZg==?=") if err != nil { panic(err) } fmt.Println(header) // Output: ู„ุฎุทูˆุงุช ุงู„ุชูŠ ุชุฌู…ุน ุจูŠู† ุญูุธ ุงู„ู‚ุฑุงู“ู† ุงู„ูƒุฑูŠู… ูˆูู‡ู…ู‡ ู…ู…ุง ุงู”ู…ู„ุงู‡ ุงู„ุนู„ุงู…ุฉ ุนุจุฏ ุงู„ู„ู‡ ุงู„ุบุฏูŠุงู†.pdf } = dec.DecodeHeader ( "= UTF- package main import ( "fmt" "mime" ) func main() { dec := new(mime.WordDecoder) header, err := dec.DecodeHeader("=?UTF-8?B?2KfZhNiu2LfZiNin2Kog2KfZhNiq2Yog2KrYrNmF2Lkg2KjZitmG?= =?UTF-8?B?INit2YHYuCDYp9mE2YLYsdin2ZPZhiDYp9mE2YPYsdmK2YUg2YjZgQ==?= =?UTF-8?B?2YfZhdmHINmF2YXYpyDYp9mU2YXZhNin2Ycg2KfZhNi52YTYp9mF?= =?UTF-8?B?2Kkg2LnYqNivINin2YTZhNmHINin2YTYutiv2YrYp9mGLnBkZg==?=") if err != nil { panic(err) } fmt.Println(header) // Output: ู„ุฎุทูˆุงุช ุงู„ุชูŠ ุชุฌู…ุน ุจูŠู† ุญูุธ ุงู„ู‚ุฑุงู“ู† ุงู„ูƒุฑูŠู… ูˆูู‡ู…ู‡ ู…ู…ุง ุงู”ู…ู„ุงู‡ ุงู„ุนู„ุงู…ุฉ ุนุจุฏ ุงู„ู„ู‡ ุงู„ุบุฏูŠุงู†.pdf } 

If you are using an older version of Go, you can use my replacement library: https://github.com/alexcesaro/quotedprintable

+2
source

Source: https://habr.com/ru/post/1214946/