How to create and parse multipart/form-data requests with MIME boundaries in Go?

To create and parse multipart/form-data requests with MIME boundaries in Go, you can use the mime/multipart package. Here's an example of how you can do it:

  1. Creating a multipart/form-data request with MIME boundaries:
package main import ( "bytes" "mime/multipart" "net/http" "fmt" ) func main() { // Create a new buffer to store the request body var requestBody bytes.Buffer // Create a new multipart writer with the buffer and a random boundary writer := multipart.NewWriter(&requestBody) // Add form field values writer.WriteField("field1", "value1") writer.WriteField("field2", "value2") // Add file(s) file, err := writer.CreateFormFile("file", "example.txt") if err != nil { fmt.Println(err) return } file.Write([]byte("File content goes here")) // Close the multipart writer writer.Close() // Create a new HTTP request with the multipart/form-data content type request, err := http.NewRequest("POST", "https://example.com/upload", &requestBody) if err != nil { fmt.Println(err) return } request.Header.Set("Content-Type", writer.FormDataContentType()) // Send the request client := &http.Client{} response, err := client.Do(request) if err != nil { fmt.Println(err) return } // Handle the response fmt.Println(response.Status) }
  1. Parsing a multipart/form-data request with MIME boundaries:
package main import ( "mime/multipart" "net/http" "fmt" ) func main() { // Handle the request http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // Check if the request content type is multipart/form-data if r.Header.Get("Content-Type") != "multipart/form-data" { fmt.Fprintln(w, "Invalid content type") return } // Parse the multipart form err := r.ParseMultipartForm(32 << 20) // Set the max memory to 32MB if err != nil { fmt.Fprintln(w, err) return } // Access the form fields field1 := r.FormValue("field1") field2 := r.FormValue("field2") fmt.Fprintf(w, "field1: %s\n", field1) fmt.Fprintf(w, "field2: %s\n", field2) // Access the uploaded file(s) file, handler, err := r.FormFile("file") if err != nil { fmt.Fprintln(w, err) return } defer file.Close() fmt.Fprintf(w, "File name: %s\n", handler.Filename) // Handle the file content buffer := make([]byte, 512) bytesRead, err := file.Read(buffer) if err != nil { fmt.Fprintln(w, err) return } fmt.Fprintf(w, "File content: %s\n", string(buffer[:bytesRead])) }) // Start the server http.ListenAndServe(":8080", nil) }

In the above examples, the first code block demonstrates how to create a multipart/form-data request with MIME boundaries. It uses the multipart.NewWriter function to create a new multipart writer and writes form field values and file content to it. The resulting requestBody is then used to create an HTTP request with the POST method and the appropriate Content-Type header.

The second code block shows how to parse a multipart/form-data request with MIME boundaries. It uses the http.Request's ParseMultipartForm method to parse the request's multipart form. The form fields and uploaded file(s) can then be accessed using the r.FormValue and r.FormFile functions, respectively.

Note: Don't forget to handle errors appropriately in your code.