Home >Backend Development >Golang >Why am I getting a 301 Moved Permanently Error when using Multipart/Form-Data POST requests?
Multipart/Form-Data POSTs
When attempting to POST data using multipart/form-data, error messages like the one provided can be encountered. Understanding the issue requires examining the problem's composition. The error encountered is a 301 Moved Permanently response, indicating that the resource has been permanently redirected. This often occurs when the correct Content-Type header is not set for multipart/form-data requests.
To resolve this issue, ensure that the Content-Type header is explicitly set to "multipart/form-data;charset=UTF-8" when making the POST request. This header informs the server that the request includes both text-based and binary data formatted according to the multipart/form-data protocol.
Below is a corrected Go code that successfully sets the correct Content-Type header:
<code class="go">import ( "bytes" "fmt" "io" "io/ioutil" "mime/multipart" "net/http" ) func NewPostWithMultipartFormData(url string, paramTexts map[string]string, paramFiles []FileItem) ([]byte, error) { // Initialize a buffer to write the multipart form data. buf := new(bytes.Buffer) // Create a new multipart writer. w := multipart.NewWriter(buf) // Add text parameters to the multipart form. for key, value := range paramTexts { field, err := w.CreateFormField(key) if err != nil { return nil, fmt.Errorf("error creating form field '%s': %v", key, err) } if _, err := field.Write([]byte(value)); err != nil { return nil, fmt.Errorf("error writing value to form field '%s': %v", key, err) } } // Add binary parameters to the multipart form. for _, file := range paramFiles { fileWriter, err := w.CreateFormFile(file.Key, file.FileName) if err != nil { return nil, fmt.Errorf("error creating form file '%s': %v", file.Key, err) } if _, err := fileWriter.Write(file.Content); err != nil { return nil, fmt.Errorf("error writing content to form file '%s': %v", file.Key, err) } } // Close the multipart writer. if err := w.Close(); err != nil { return nil, fmt.Errorf("error closing multipart writer: %v", err) } contentType := w.FormDataContentType() // Create a new POST request with the correct Content-Type header. req, err := http.NewRequest(http.MethodPost, url, buf) if err != nil { return nil, fmt.Errorf("error creating HTTP request: %v", err) } req.Header.Set("Content-Type", contentType) // Perform the HTTP request. client := http.Client{} resp, err := client.Do(req) if err != nil { return nil, fmt.Errorf("error sending HTTP request: %v", err) } defer resp.Body.Close() // Read the response body. body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("error reading HTTP response body: %v", err) } return body, nil }</code>
The above is the detailed content of Why am I getting a 301 Moved Permanently Error when using Multipart/Form-Data POST requests?. For more information, please follow other related articles on the PHP Chinese website!