解析包含文件内容的POST请求体

huangapple go评论80阅读模式
英文:

Parsing POST request body containing file content

问题

我组合了一个HTTP请求来发送文件内容:

// HTTP请求。
req, err := UploadRequest("/slice", "file", pth)

使用以下函数:

// 创建一个新的文件上传HTTP请求。
// https://gist.github.com/mattetti/5914158/f4d1393d83ebedc682a3c8e7bdc6b49670083b84
func UploadRequest(uri string, paramName, path string) (*http.Request, error) {
	file, err := os.Open(path) // 处理错误...
	fileContents, err := ioutil.ReadAll(file) // 处理错误...
	fi, err := file.Stat() // 处理错误...
	file.Close()

	body := new(bytes.Buffer)
	writer := multipart.NewWriter(body)
	part, err := writer.CreateFormFile(paramName, fi.Name()) // 处理错误...
	part.Write(fileContents)

	err = writer.Close() // 处理错误...

	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) // 我有请求体 :)
    }
}

调试器显示请求的body是:

解析包含文件内容的POST请求体

我想在\r\n\r\n字符之后获取请求体数据。我该如何做到这一点?

尝试过的方法

我尝试过以下方法,但没有成功:

err = r.ParseForm() // 处理错误...
stl := r.PostForm.Get("file") // "file"参数名是硬编码的。

// `stl`只是一个空字符串。
英文:

I compose an HTTP request to send file content:

// HTTP request.
req, err := UploadRequest("/slice", "file", pth)

By this function:

// Creates a new file upload http request.
// https://gist.github.com/mattetti/5914158/f4d1393d83ebedc682a3c8e7bdc6b49670083b84
func 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
}

Question

The request handler receives the request body:

func Handler(w http.ResponseWriter, r *http.Request) {
    switch r.Method {
	case "POST":
		body, err := ioutil.ReadAll(r.Body) // I have request body :)
    }
}

Debugger shows the request body is:

解析包含文件内容的POST请求体

I'm trying to get the body data after \r\n\r\n characters. How can I do that?

Tried

This is tried, but didn't work:

err = r.ParseForm() // Handle err...
stl := r.PostForm.Get("file") // "file" param name is hard-coded.

// `stl` is just an empty string.

答案1

得分: 2

使用Request.FormFile来解析多部分请求体,并返回其中的文件:

func Handler(w http.ResponseWriter, r *http.Request) {
    switch r.Method {
    case "POST":
        f, h, err := r.FormFile(paramName)
        if err != nil {
            // TODO: 处理错误
        }
        data, err := ioutil.ReadAll(f)
        if err != nil {
            // TODO: 处理错误
        }
    }
}

请注意,上述代码中的paramName应该替换为实际的参数名称。

英文:

Use Request.FormFile to parse the multipart request body and return the file within:

func Handler(w http.ResponseWriter, r *http.Request) {
    switch r.Method {
    case "POST":
        f, h, err := r.FormFile(paramName)
        if err != nil {
            // TODO: Handle error
        }
        data, err := ioutil.ReadAll(f)
        if err != nil {
            // TODO: Handle error
        }
    }
}

huangapple
  • 本文由 发表于 2022年1月5日 15:44:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/70589361.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定