From 051abe57737de7f6d327e5a847d95716b2e13678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Mon, 16 Oct 2023 12:25:55 +0900 Subject: [PATCH] fix multipart --- http.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/http.go b/http.go index 005d4ea..166167c 100644 --- a/http.go +++ b/http.go @@ -122,11 +122,10 @@ func WriteBodyByMultipartFormData(params ...any) (*bytes.Buffer, string, error) continue } h := make(textproto.MIMEHeader) - h.Set("Content-Disposition", - fmt.Sprintf(`form-data; name="%s"; filename="%s"`, - escapeQuotes(fieldname), escapeQuotes(fieldname))) if rx.Kind() == reflect.Pointer && rx.Elem().Kind() == reflect.Struct { // 使用 json 编码 - h.Set("Content-Type", "application/octet-stream") + h.Set("Content-Disposition", + fmt.Sprintf(`form-data; name="%s"`, escapeQuotes(fieldname))) + h.Set("Content-Type", "application/json") r, err := w.CreatePart(h) if err != nil { return nil, "", err @@ -140,6 +139,9 @@ func WriteBodyByMultipartFormData(params ...any) (*bytes.Buffer, string, error) switch o := x.(type) { case string: if strings.HasPrefix(o, "file:///") { // 是文件路径 + h.Set("Content-Disposition", + fmt.Sprintf(`form-data; name="%s"; filename="%s"`, + escapeQuotes(fieldname), escapeQuotes(fieldname))) h.Set("Content-Type", "application/octet-stream") r, err := w.CreatePart(h) if err != nil { @@ -157,6 +159,9 @@ func WriteBodyByMultipartFormData(params ...any) (*bytes.Buffer, string, error) continue } if strings.HasPrefix(o, "base64://") { // 是 base64 + h.Set("Content-Disposition", + fmt.Sprintf(`form-data; name="%s"; filename="%s"`, + escapeQuotes(fieldname), escapeQuotes(fieldname))) h.Set("Content-Type", "application/octet-stream") r, err := w.CreatePart(h) if err != nil { @@ -169,6 +174,9 @@ func WriteBodyByMultipartFormData(params ...any) (*bytes.Buffer, string, error) continue } if strings.HasPrefix(o, "base16384://") { // 是 base16384 + h.Set("Content-Disposition", + fmt.Sprintf(`form-data; name="%s"; filename="%s"`, + escapeQuotes(fieldname), escapeQuotes(fieldname))) h.Set("Content-Type", "application/octet-stream") r, err := w.CreatePart(h) if err != nil { @@ -180,6 +188,9 @@ func WriteBodyByMultipartFormData(params ...any) (*bytes.Buffer, string, error) } continue } + h.Set("Content-Disposition", + fmt.Sprintf(`form-data; name="%s"`, escapeQuotes(fieldname))) + h.Set("Content-Type", "application/json") r, err := w.CreatePart(h) if err != nil { return nil, "", err @@ -190,6 +201,9 @@ func WriteBodyByMultipartFormData(params ...any) (*bytes.Buffer, string, error) } continue case []byte: + h.Set("Content-Disposition", + fmt.Sprintf(`form-data; name="%s"; filename="%s"`, + escapeQuotes(fieldname), escapeQuotes(fieldname))) h.Set("Content-Type", "application/octet-stream") r, err := w.CreatePart(h) if err != nil { @@ -201,6 +215,9 @@ func WriteBodyByMultipartFormData(params ...any) (*bytes.Buffer, string, error) } continue default: + h.Set("Content-Disposition", + fmt.Sprintf(`form-data; name="%s"`, escapeQuotes(fieldname))) + h.Set("Content-Type", "application/json") r, err := w.CreatePart(h) if err != nil { return nil, "", err