Skip to content

Instantly share code, notes, and snippets.

@andrewmilson
Last active June 25, 2026 14:36
Show Gist options
  • Select an option

  • Save andrewmilson/19185aab2347f6ad29f5 to your computer and use it in GitHub Desktop.

Select an option

Save andrewmilson/19185aab2347f6ad29f5 to your computer and use it in GitHub Desktop.
Golang multipart/form-data File Upload
package main
import (
"net/http"
"os"
"bytes"
"path"
"path/filepath"
"mime/multipart"
"io"
)
func main() {
fileDir, _ := os.Getwd()
fileName := "upload-file.txt"
filePath := path.Join(fileDir, fileName)
file, _ := os.Open(filePath)
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, _ := writer.CreateFormFile("file", filepath.Base(file.Name()))
io.Copy(part, file)
writer.Close()
r, _ := http.NewRequest("POST", "http://example.com", body)
r.Header.Add("Content-Type", writer.FormDataContentType())
client := &http.Client{}
client.Do(r)
}
@Altiano

Altiano commented May 14, 2020

Copy link
Copy Markdown

Thanks!

@hhsm95

hhsm95 commented Jun 2, 2020

Copy link
Copy Markdown

This example is the best. Thanks!

@jingyugao

Copy link
Copy Markdown

Will this code use sendfile to achieve zero copy?

@EvgTG

EvgTG commented Nov 3, 2020

Copy link
Copy Markdown

Спасибо)

@LIMHARRY

LIMHARRY commented Mar 4, 2021

Copy link
Copy Markdown

Thank you soooooooooo much!!

@magarem

magarem commented May 15, 2021

Copy link
Copy Markdown

thanks!! :)

@kabaluyot

Copy link
Copy Markdown

alright!

@irvanrahadhian

Copy link
Copy Markdown

Thank you

@yedamao

yedamao commented Aug 6, 2021

Copy link
Copy Markdown

👍

@MingyiLiang

Copy link
Copy Markdown

Do you have any ideas how to send a zip file using multipart writer?

@AlfarisiN

Copy link
Copy Markdown

Thanks!! Much appreciated!

@foofybuster

Copy link
Copy Markdown

Thanks! it's working well.

@AnjanaMadu

Copy link
Copy Markdown

Thanks!

@lsdrfrx

lsdrfrx commented May 12, 2022

Copy link
Copy Markdown

Thank you <3

@jonatan-holmgren

Copy link
Copy Markdown

Thanks.

@devnote-dev

Copy link
Copy Markdown

Thanks 👍

@pablodz

pablodz commented May 31, 2022

Copy link
Copy Markdown

it works

@zhangpengspin

Copy link
Copy Markdown

it works

@Sujaybiradar25

Copy link
Copy Markdown

Thanks!

@mulder-phenos

Copy link
Copy Markdown

Nice work

@defaultGitCoder

Copy link
Copy Markdown

Thx, nice gist

@episey

episey commented Sep 29, 2022

Copy link
Copy Markdown

Solid

@cadecuddy

Copy link
Copy Markdown

thanks!

@toudi

toudi commented Oct 18, 2022

Copy link
Copy Markdown

if, for whatever reason you need to specify the content type of a part - here's the excerpt:

body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, "file", "myfile.xml"))
h.Set("Content-Type", "text/xml")
part, _ := writer.CreatePart(h)

otherwise this code is absolutely perfect, thanks !

@gabrielNetto94

Copy link
Copy Markdown

thanks!

@lnstchtped

Copy link
Copy Markdown

thanks!

@Serares

Serares commented Jan 14, 2023

Copy link
Copy Markdown

Is it possible to compile this in an executable and then whenever you run that '.exe' the server will start to listen on incoming requests?

@pforpramit

Copy link
Copy Markdown

Hello, if I need to upload a zip file as form-data in POST request body, I am able to find this,

zipWriter := zip.NewWriter(buf)
zipFile, err := zipWriter.Create(fileName)

Unlike multipart.Writer, in case of zip.Writer I can't find any option to create form file in a key-value fashion.

How can I achieve this for zip as well?

@PzaThief

Copy link
Copy Markdown

Will this code use sendfile to achieve zero copy?

No It doesn't.
If you want to achieve zero copy, you can use os.pipe instead of sendfile.
sendfile is fit to tcp package so, it is hard to use with http package.
See this code snippet and benchmarks of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment