Skip to content

Commit

Permalink
Use form headers in WriteMultipartForm
Browse files Browse the repository at this point in the history
CreateFormFile always set Content-Type header to application/octet-stream.
Use CreatePart instead so all headers in the multipart form can be used.
  • Loading branch information
tommy351 authored and kirillDanshin committed Oct 5, 2018
1 parent 4dfc129 commit d459e25
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ func WriteMultipartForm(w io.Writer, f *multipart.Form, boundary string) error {
// marshal files
for k, fvv := range f.File {
for _, fv := range fvv {
vw, err := mw.CreateFormFile(k, fv.Filename)
vw, err := mw.CreatePart(fv.Header)
if err != nil {
return fmt.Errorf("cannot create form file %q (%q): %s", k, fv.Filename, err)
}
Expand Down
29 changes: 29 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1811,3 +1811,32 @@ func createChunkedBody(body []byte) []byte {
}
return append(b, []byte("0\r\n\r\n")...)
}

func TestWriteMultipartForm(t *testing.T) {
var w bytes.Buffer
s := strings.Replace(`--foo
Content-Disposition: form-data; name="key"
value
--foo
Content-Disposition: form-data; name="file"; filename="test.json"
Content-Type: application/json
{"foo": "bar"}
--foo--
`, "\n", "\r\n", -1)
mr := multipart.NewReader(strings.NewReader(s), "foo")
form, err := mr.ReadForm(1024)

if err != nil {
t.Fatalf("unexpected error: %s", err)
}

if err := WriteMultipartForm(&w, form, "foo"); err != nil {
t.Fatalf("unexpected error: %s", err)
}

if string(w.Bytes()) != s {
t.Fatalf("unexpected output %q", w.Bytes())
}
}

0 comments on commit d459e25

Please sign in to comment.