Browse Source

feat: 添加form-data的支持

SongZihuan 1 year ago
parent
commit
198bf833e2
2 changed files with 47 additions and 13 deletions
  1. 43 13
      rest/httpx/util.go
  2. 4 0
      rest/internal/header/headers.go

+ 43 - 13
rest/httpx/util.go

@@ -1,30 +1,60 @@
 package httpx
 
-import "net/http"
+import (
+	"fmt"
+	"github.com/wuntsong-org/go-zero-plus/rest/internal/header"
+	"net/http"
+	"strings"
+)
 
 const xForwardedFor = "X-Forwarded-For"
 
 // GetFormValues returns the form values.
 func GetFormValues(r *http.Request) (map[string]any, error) {
-	if err := r.ParseForm(); err != nil {
-		return nil, err
-	}
+	if strings.Contains(r.Header.Get(header.ContentType), header.FormDataType) {
+		err := r.ParseMultipartForm(maxMemory)
+		if err != nil {
+			return nil, err
+		}
+
+		if r.MultipartForm == nil {
+			return nil, fmt.Errorf("not form-data")
+		}
+
+		params := make(map[string]any, len(r.MultipartForm.Value))
+		for name, value := range r.MultipartForm.Value {
+			if len(value) != 1 {
+				continue
+			}
+
+			formValue := value[0]
+			if len(formValue) == 0 {
+				continue
+			}
 
-	if err := r.ParseMultipartForm(maxMemory); err != nil {
-		if err != http.ErrNotMultipart {
+			params[name] = formValue
+		}
+
+		return params, nil
+	} else if strings.Contains(r.Header.Get(header.ContentType), header.FormUrlEncodedType) {
+		err := r.ParseForm()
+		if err != nil {
 			return nil, err
 		}
-	}
 
-	params := make(map[string]any, len(r.Form))
-	for name := range r.Form {
-		formValue := r.Form.Get(name)
-		if len(formValue) > 0 {
+		params := make(map[string]any, len(r.Form))
+		for name := range r.Form {
+			formValue := r.Form.Get(name)
+			if len(formValue) == 0 {
+				continue
+			}
 			params[name] = formValue
 		}
-	}
 
-	return params, nil
+		return params, nil
+	} else {
+		return make(map[string]any, 0), nil
+	}
 }
 
 // GetRemoteAddr returns the peer address, supports X-Forward-For.

+ 4 - 0
rest/internal/header/headers.go

@@ -7,4 +7,8 @@ const (
 	ContentType = "Content-Type"
 	// JsonContentType is the content type for JSON.
 	JsonContentType = "application/json; charset=utf-8"
+	// FormUrlEncodedType is the content type for Form Url Encoded
+	FormUrlEncodedType = "application/x-www-form-urlencoded"
+	// FormDataType is the content type for Form-Data
+	FormDataType = "multipart/form-data"
 )