Beego uses func Request.ParseMultipartForm function on input.go; however Go does not recommend it

871 Views Asked by At

Because of ParseFormOrMulitForm function in Beego, I got an ERROR:

Handler crashed with error http: multipart handled by ParseMultipartForm

Beego uses Request.ParseMultipartForm function in input.go.

// ParseFormOrMulitForm parseForm or parseMultiForm based on Content-type
func (input *BeegoInput) ParseFormOrMulitForm(maxMemory int64) error {
    // Parse the body depending on the content type.
    if strings.Contains(input.Header("Content-Type"), "multipart/form-data") {
        if err := input.Context.Request.ParseMultipartForm(maxMemory); err != nil {
            return errors.New("Error parsing request body:" + err.Error())
        }
    } else if err := input.Context.Request.ParseForm(); err != nil {
        return errors.New("Error parsing request body:" + err.Error())
    }
    return nil
}

However, I found that Go does not recommend to use Request.ParseMultipartForm for stream processing.

https://golang.org/src/net/http/request.go:

Use MultipartReader function instead of ParseMultipartForm to process the request body as a stream.

362 // MultipartReader returns a MIME multipart reader if this is a
363 // multipart/form-data POST request, else returns nil and an error.
364 // Use this function instead of ParseMultipartForm to
365 // process the request body as a stream.
366 func (r *Request) MultipartReader() (*multipart.Reader, error) {
367     if r.MultipartForm == multipartByReader {
368         return nil, errors.New("http: MultipartReader called twice")
369     }
370     if r.MultipartForm != nil {
371         return nil, errors.New("http: multipart handled by ParseMultipartForm")
372     }
373     r.MultipartForm = multipartByReader
374     return r.multipartReader()
375 }
376 

How to fix it?

0

There are 0 best solutions below