1
0
mirror of https://github.com/fumiama/NanoBot.git synced 2026-06-06 03:00:24 +08:00

fix multipart

This commit is contained in:
源文雨
2023-10-16 12:25:55 +09:00
parent 8cb79c5058
commit 051abe5773

25
http.go
View File

@@ -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