Home > Net >  I can't add a Header to a specific multipart part in golang
I can't add a Header to a specific multipart part in golang

Time:12-18

I'm using an API that requires the content-type of a multipart form to be Content-Type: audio/wav but if you add a file with part, _ := writer.CreateFormFile("audio_file", "test2.wav") it makes the content-type application/octet-stream

I've tried: part.Header.Set("Content-Type", "audio/wav") but Header is undefined.

Here is the curl request data minus the binary that works:

Content-Disposition: form-data; name="audio_file"; filename="test2.wav"
Content-Type: audio/wav

Here is my request minus the binary data that is rejected:

Content-Disposition: form-data; name="audio_file"; filename="test2.wav"
Content-Type: application/octet-stream

CodePudding user response:

Call CreatePart directly instead of the CreateFormFile convenience method. Set the content type in the header used to create the part.

h := make(textproto.MIMEHeader)
h.Set("Content-Disposition",
    fmt.Sprintf(`form-data; name="%s"; filename="%s"`, "audio_file", "test2.wav"))
h.Set("Content-Type", "audio/wav")
part, err := writer.CreatePart(h)
  • Related