Dealing with multipart HTTP data
When reading HTTP multipart data, using Request.FormFile
to get
files from the request may not be the best way. It causes the http
package read the body, and store data in temporary files if data is
larger than 10MB. To prevent that, you have to use the multipart
package and read the body yourself.
Multipart body is separated by a boundary
string, which can be
obtained from the Content-Type
:
|
|
Once the boundary is known, you can create a multipart reader:
|
|
Creating multipart payload is a bit more involved. First, create a multipart writer:
|
|
Then, create and add the parts:
|
|
Build the request:
|
|
If you need to stream the files to the request, then you need to create a pipe, and build the multipart content using a separate goroutine.