On Thu, 6 Apr 2017 04:00:20 -0700 (PDT) Ain <ain.val...@gmail.com> wrote:
> I get string which contains: > > MIME-Version: 1.0 > content-type: text/xml > content-transfer-encoding: base64 > > PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPD94bWwtc3R5bGVzaGVldCB0 > ... > > > ie there are some headers and then base64 encoded data. I suspect > there might be some functions in the std lib which should be able to > parse this and give me easy access to the headers and data but I just > can't find it... it isn't mime/multipart, right? Perhaps something > in the http client package? Almost. What you're dealing with is a typical set of headers of a so-called "MIME-formatted" e-mail message followed by its body. The usual set of headers is missing (those 'From', 'To' etc) and that's why it looks strange. So you parse it like a regular mail message using the net/mail package and then base64-decode its body. The program (playground link is [1]) ----------------8<---------------- package main import ( "bytes" "encoding/base64" "fmt" "io" "net/mail" "strings" ) const s = `MIME-Version: 1.0 content-type: text/xml content-transfer-encoding: base64 PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPD94bWwtc3R5bGVzaGVldCB0 ` func main() { sr := strings.NewReader(s) msg, err := mail.ReadMessage(sr) if err != nil { panic(err) } fmt.Printf("%#v\n", msg.Header) var buf bytes.Buffer dec := base64.NewDecoder(base64.StdEncoding, msg.Body) _, err = io.Copy(&buf, dec) if err != nil { panic(err) } fmt.Println(buf.String()) } ----------------8<---------------- outputs: | mail.Header{"Content-Type":[]string{"text/xml"}, "Content-Transfer-Encoding":[]string{"base64"}, "Mime-Version": []string{"1.0"}} | <?xml version="1.0" encoding="UTF-8"?> | <?xml-stylesheet t Which is indeed base64-encoded XML document. As shown, you can (should?) inspect the parsed headers to know which content type the decoded document really is (say, if in the future your source would sent Content-Type: text/json you'll be better prepared for this). 1. https://play.golang.org/p/lOKAAfQRs8 -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.