Home >Backend Development >Golang >How to Create Multipart HTTP Requests with Gomultipart in Golang?
multipart in Golang is a powerful tool for creating multipart HTTP requests. This can be particularly useful when sending data that includes both text and file content.
To create a multipart form request, follow these steps:
In your example, you would create a multipart mixed request as follows:
<code class="go">body := &bytes.Buffer{} writer := multipart.NewWriter(body) part, err := writer.CreatePart(textproto.MIMEHeader{"Content-Type": {"application/json"}}) if err != nil { // handle error } part.Write(jsonStr) writer.Close() req, err := http.NewRequest("POST", "blabla", body) if err != nil { // handle error } req.Header.Set("Content-Type", "multipart/mixed; boundary="+writer.Boundary())</code>
You can also generate a multipart request using cURL with the following command:
curl -F "field=value" -H "Content-Type: multipart/mixed; boundary=boundary" http://1.1.1.1/blabla
The above is the detailed content of How to Create Multipart HTTP Requests with Gomultipart in Golang?. For more information, please follow other related articles on the PHP Chinese website!