Parsing base64 data with headers

1,242 views
Skip to first unread message

Ain

unread,
Apr 6, 2017, 7:00:21 AM4/6/17
to golang-nuts
Hi

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?

TIA
ain

chris dollin

unread,
Apr 6, 2017, 7:07:13 AM4/6/17
to Ain, golang-nuts
On 6 April 2017 at 12:00, Ain <ain.v...@gmail.com> wrote:

> 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?

encoding/base64?

Chris

--
Chris "allusive" Dollin

Ain

unread,
Apr 6, 2017, 7:16:02 AM4/6/17
to golang-nuts, ain.v...@gmail.com
Yes, it could be used to decode the base64 data, but the "message" I have has some headers in the beginning (which the encoding/base64 wouldn't handle AFAIK). I'm hoping that there is already some func which handles parsing such a message as is.


ain

Konstantin Khomoutov

unread,
Apr 6, 2017, 7:51:13 AM4/6/17
to Ain, golang-nuts
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

dja...@gmail.com

unread,
Apr 6, 2017, 7:53:18 AM4/6/17
to golang-nuts

Ain

unread,
Apr 6, 2017, 8:07:23 AM4/6/17
to golang-nuts, ain.v...@gmail.com

neljapäev, 6. aprill 2017 14:51.13 UTC+3 kirjutas Konstantin Khomoutov:
On Thu, 6 Apr 2017 04:00:20 -0700 (PDT)
Ain <ain.v...@gmail.com> wrote:

>
> 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])


Thank you, that's exactly what I was looking for!


ain

Konstantin Khomoutov

unread,
Apr 6, 2017, 8:10:13 AM4/6/17
to Ain, golang-nuts
On Thu, 6 Apr 2017 14:50:43 +0300
Konstantin Khomoutov <flat...@users.sourceforge.net> wrote:

> > I get string which contains:
[...]
> 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.
[...]
> 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).
[...]

Also note that depending on your source it might indeed be possible to
get data with the value of the "Content-Type" header set to one
of "multipart" types [2] in which case the message's body will be text
delimited into a set of parts each of which will have its own set of
headers, including the Content-Type header. Dealing with such payloads
is what mime/multipart is for.

I don't know your situation but I'd assert the headers you parse have
expected values (i.e. content type is "text/xml" or "application/xml"
etc, and the content transfer encoding is indeed base64).

2. https://en.wikipedia.org/wiki/MIME#Content-Type
Reply all
Reply to author
Forward
0 new messages