To make a HTTP request with a custom body as multipart form data in Go, you can follow the below steps:
bytes.Buffer
object to write your multipart body to.multipart.Writer
object, passing the bytes.Buffer
object as an argument.CreateFormFile
method of the multipart.Writer
object to create a new form file with its field name, MIME type and file name as arguments. This will return a io.Writer
object which will accept the content of the file.io.Writer
object.io.Writer
object and check for any errors.http.NewRequest
function. Set the HTTP method, URL and any headers you need for your request.ContentType
field of the multipart.Writer
object.bytes.Buffer
object by calling its Bytes
method.http.DefaultClient.Do
function to execute the request.Here's an example code snippet:
main.go1081 chars50 lines
gistlibby LogSnag