This commit is contained in:
src committed 2026-09-28 11:17:52 +03:00
1 parent d340d1b2ad
commit 0a9202c7c0
100 files changed
+2064 -330

No files matched your search

+9 -5
View File
@@ -114,6 +114,7 @@ func (c *Client) PrepareRequest(
if body != nil {
return nil, errors.New("cannot specify requestBody and multipart form at the same time.")
}
pipeReader, pipeWriter := io.Pipe()
writer := multipart.NewWriter(pipeWriter)
headerParams["Content-Type"] = writer.FormDataContentType()
@@ -133,6 +134,7 @@ func (c *Client) PrepareRequest(
}
}
}
for _, formFile := range formFiles {
var part io.Writer
part, err = writer.CreateFormFile(formFile.FieldName, filepath.Base(formFile.Filename))
@@ -143,8 +145,10 @@ func (c *Client) PrepareRequest(
return
}
}
err = writer.Close()
}()
closeMultipart = func(err error) {
pipeReader.CloseWithError(err)
pipeWriter.CloseWithError(err)
@@ -155,6 +159,7 @@ func (c *Client) PrepareRequest(
if body != nil {
return nil, errors.New("cannot specify requestBody and x-www-form-urlencoded form at the same time.")
}
formBody := &bytes.Buffer{}
if _, err := formBody.WriteString(formParams.Encode()); err != nil {
return nil, err
@@ -223,27 +228,26 @@ func setBody(body any, contentType string) (bodyBuffer *bytes.Buffer, err error)
_, err = bodyBuffer.ReadFrom(reader)
} else if bytesValue, ok := body.([]byte); ok {
_, err = bodyBuffer.Write(bytesValue)
} else if jsonCheck.MatchString(contentType) {
err = json.NewEncoder(bodyBuffer).Encode(body)
} else if stringValue, ok := body.(string); ok {
_, err = bodyBuffer.WriteString(stringValue)
} else if stringValue, ok := body.(*string); ok {
_, err = bodyBuffer.WriteString(*stringValue)
} else if jsonCheck.MatchString(contentType) {
err = json.NewEncoder(bodyBuffer).Encode(body)
} else if xmlCheck.MatchString(contentType) {
var encoded []byte
encoded, err = xml.Marshal(body)
if err == nil {
bodyBuffer.Write(encoded)
}
} else {
return nil, fmt.Errorf("invalid body type %s\n", contentType)
}
if err != nil {
return nil, err
}
if bodyBuffer.Len() == 0 {
return nil, fmt.Errorf("invalid body type %s\n", contentType)
}
return bodyBuffer, nil
}