Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bramhaghosh/4b4a603c4cc50e09da37b54e99bdbad7 to your computer and use it in GitHub Desktop.
Save bramhaghosh/4b4a603c4cc50e09da37b54e99bdbad7 to your computer and use it in GitHub Desktop.
mulitpart range req response
var mpbody bytes.Buffer
mpw := multipart.NewWriter(&mpbody)
defer mpw.Close()
mpw.SetBoundary(m.boundary)
for _, rng := range ranges {
start, end := rng[0], rng[1]
mimeh := make(textproto.MIMEHeader)
mimeh.Set("Content-Type", m.contentType)
mimeh.Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, end, len(m.body)))
seg := m.body[start : end+1]
pw, err := mpw.CreatePart(mimeh)
if err != nil {
return err
}
if _, err := pw.Write(seg); err != nil {
return err
}
}
res.ContentLength = int64(len(mpbody.Bytes()))
res.Body = ioutil.NopCloser(bytes.NewReader(mpbody.Bytes()))
res.Header.Set("Content-Type", fmt.Sprintf("multipart/byteranges; boundary=%s", m.boundary))
return nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment