In this article, php editor Xinyi will introduce you to the solution on how to add specific multi-part headers in golang. Headers are key components in HTTP requests and responses, used to convey various metadata information. However, sometimes we may encounter an issue that specific multipart headers cannot be added to the request in golang. In what follows, we’ll provide a simple and effective solution to help you overcome this problem. let's start!
The api I use requires the content-type of the multipart form to be content-type: audio/wav but if you add a file
part, _ := writer.createformfile("audio_file", "test2.wav")
It makes the content type application/octet-stream
I have tried before:
part.header.set("content-type", "audio/wav")
But the header is not defined.
This is the curl request data minus the valid binary:
content-disposition: form-data; name="audio_file"; filename="test2.wav" content-type: audio/wav
Here is my request minus the rejected binary data:
Content-Disposition: form-data; name="audio_file"; filename="test2.wav" Content-Type: application/octet-stream
Call createpart directly instead of the createformfile convenience method. Set the content type in the header used to create the widget.
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)
The above is the detailed content of I can't add header to specific multipart in golang. For more information, please follow other related articles on the PHP Chinese website!