問題:
如何傳送內容類型為 multipart/form-data 的 POST 請求,同時包含 []byte 參數和字串參數?之前的嘗試導致了以下錯誤:
[301 301 Moved Permanently]<...HTML response body...>
提供的Go 代碼:
<code class="go">func NewPost2(url string) ([]byte, error) { m := make(map[string]interface{}, 0) m["fileName"] ="good" m["name"] = Base64ToByte("/9j/4AAQSkZJRgABAQEAeAB4AAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAABAAEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDHooor+wD+Zz//2Q==") b, _ := json.Marshal(m) httpReq, err := http.NewRequest("POST", url, bytes.NewBuffer(b)) httpReq.Header.Set("Content-Type", "multipart/form-data;charset=UTF-8") client := &http.Client{} resp, err := client.Do(httpReq) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode < 200 || resp.StatusCode >= 300 { b, _ := ioutil.ReadAll(resp.Body) return nil, fmt.Errorf("[%d %s]%s", resp.StatusCode, resp.Status, string(b)) } respData, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } return respData, nil }</code>
答案:
提供的程式碼嘗試在請求正文中以JSON 形式發送[]byte 參數,這不是multipart/form-data 提交的合適格式。
要修正此問題,我們可以使用multipart/form-使用以下更新程式碼進行資料編碼:
<code class="go">func NewPostFile(url string, paramTexts map[string]interface{}, paramFile FileItem) ([]byte, error) { // if paramFiles ==nil { // return NewPost(url,paramTexts,header,transport) // } // Create a new multipart writer bodyBuf := &bytes.Buffer{} bodyWriter := multipart.NewWriter(bodyBuf) // Write text parameters to the multipart writer for k, v := range paramTexts { bodyWriter.WriteField(k, v.(string)) } // Write the file content to the multipart writer fileWriter, err := bodyWriter.CreateFormFile(paramFile.Key, paramFile.FileName) if err != nil { fmt.Println(err) //fmt.Println("Create form file error: ", error) return nil, err } fileWriter.Write(paramFile.Content) // Close the multipart writer bodyWriter.Close() // Set the Content-Type header to multipart/form-data contentType := bodyWriter.FormDataContentType() // Create the POST request with the multipart/form-data body resp, err := http.Post(url, contentType, bodyBuf) if err != nil { return nil, err } // Handle and process the HTTP response // ... (remainder of code omitted for brevity)</code>
透過使用multipart/form-data 編碼並正確處理檔案參數,我們現在可以使用[]byte 參數和字串參數成功POST。
以上是鑑於我之前的嘗試導致了 301 Moved Pe,如何在 Go 中發送 Content-Type: multipart/form-data 的 POST 請求,同時包含 []byte 參數和字串參數的詳細內容。更多資訊請關注PHP中文網其他相關文章!