This commit is contained in:
gospider
2025-04-02 11:02:19 +08:00
parent 9da7a11bee
commit f11d71a718
2 changed files with 7 additions and 5 deletions

View File

@@ -247,7 +247,7 @@ func (obj *OrderData) parseParams() *bytes.Buffer {
}
return buf
}
func (obj *OrderData) parseForm(ctx context.Context) (io.Reader, bool, error) {
func (obj *OrderData) parseForm(ctx context.Context, boundary string) (io.Reader, bool, error) {
if len(obj.data) == 0 {
return nil, false, nil
}
@@ -265,6 +265,7 @@ func (obj *OrderData) parseForm(ctx context.Context) (io.Reader, bool, error) {
}
body := bytes.NewBuffer(nil)
writer := multipart.NewWriter(body)
writer.SetBoundary(boundary)
err := obj.formWriteMain(writer)
if err != nil {
return nil, false, err

View File

@@ -102,14 +102,14 @@ type File struct {
ContentType string
}
func randomBoundary() string {
func randomBoundary() (string, string) {
var buf [30]byte
io.ReadFull(rand.Reader, buf[:])
boundary := fmt.Sprintf("%x", buf[:])
if strings.ContainsAny(boundary, `()<>@,;:\"/[]?= `) {
boundary = `"` + boundary + `"`
}
return "multipart/form-data; boundary=" + boundary
return "multipart/form-data; boundary=" + boundary, boundary
}
func (obj *RequestOption) initBody(ctx context.Context) (io.Reader, error) {
@@ -127,8 +127,9 @@ func (obj *RequestOption) initBody(ctx context.Context) (io.Reader, error) {
}
return bytes.NewReader(con), nil
} else if obj.Form != nil {
var boundary string
if obj.ContentType == "" {
obj.ContentType = randomBoundary()
obj.ContentType, boundary = randomBoundary()
}
body, orderData, err := obj.newBody(obj.Form)
if err != nil {
@@ -137,7 +138,7 @@ func (obj *RequestOption) initBody(ctx context.Context) (io.Reader, error) {
if body != nil {
return body, nil
}
body, once, err := orderData.parseForm(ctx)
body, once, err := orderData.parseForm(ctx, boundary)
if err != nil {
return nil, err
}