我編寫了一個 HTTP 請求來發(fā)送文件內(nèi)容:// HTTP request.req, err := UploadRequest("/slice", "file", pth)通過這個函數(shù):// Creates a new file upload http request.// https://gist.github.com/mattetti/5914158/f4d1393d83ebedc682a3c8e7bdc6b49670083b84func UploadRequest(uri string, paramName, path string) (*http.Request, error) { file, err := os.Open(path) // handle err... fileContents, err := ioutil.ReadAll(file) // handle err... fi, err := file.Stat() // handle err... file.Close() body := new(bytes.Buffer) writer := multipart.NewWriter(body) part, err := writer.CreateFormFile(paramName, fi.Name()) // handle err... part.Write(fileContents) err = writer.Close() // handle err... request, err := http.NewRequest("POST", uri, body) request.Header.Add("Content-Type", writer.FormDataContentType()) return request, err}問題請求處理程序接收請求正文:func Handler(w http.ResponseWriter, r *http.Request) { switch r.Method { case "POST": body, err := ioutil.ReadAll(r.Body) // I have request body :) }}調(diào)試器顯示請求body是:我試圖在\r\n\r\n字符之后獲取身體數(shù)據(jù)。我怎樣才能做到這一點?試過了這是嘗試過的,但沒有奏效:err = r.ParseForm() // Handle err...stl := r.PostForm.Get("file") // "file" param name is hard-coded.// `stl` is just an empty string.http去郵政
解析包含文件內(nèi)容的 POST 請求正文
長風(fēng)秋雁
2022-10-24 15:07:35