2026-03-08 20:19:40 +08:00
|
|
|
|
package starnet
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"io"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// SetBody 设置请求体(字节)
|
|
|
|
|
|
func (r *Request) SetBody(body []byte) *Request {
|
|
|
|
|
|
if r.err != nil {
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
if r.doRaw {
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
2026-04-19 15:39:51 +08:00
|
|
|
|
setBytesBodyConfig(&r.config.Body, body)
|
|
|
|
|
|
r.invalidatePreparedState()
|
2026-03-08 20:19:40 +08:00
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-19 15:39:51 +08:00
|
|
|
|
// SetBodyReader 设置请求体(Reader)。
|
|
|
|
|
|
// 出于避免重复写的保守策略,Reader 形态的 body 在非幂等方法上不会自动参与 retry。
|
2026-03-08 20:19:40 +08:00
|
|
|
|
func (r *Request) SetBodyReader(reader io.Reader) *Request {
|
|
|
|
|
|
if r.err != nil {
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
if r.doRaw {
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
2026-04-19 15:39:51 +08:00
|
|
|
|
setReaderBodyConfig(&r.config.Body, reader)
|
|
|
|
|
|
r.invalidatePreparedState()
|
2026-03-08 20:19:40 +08:00
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SetBodyString 设置请求体(字符串)
|
|
|
|
|
|
func (r *Request) SetBodyString(body string) *Request {
|
|
|
|
|
|
return r.SetBody([]byte(body))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SetJSON 设置 JSON 请求体
|
|
|
|
|
|
func (r *Request) SetJSON(v interface{}) *Request {
|
|
|
|
|
|
if r.err != nil {
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
data, err := json.Marshal(v)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
r.err = wrapError(err, "marshal json")
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return r.SetContentType(ContentTypeJSON).SetBody(data)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SetFormData 设置表单数据(覆盖)
|
|
|
|
|
|
func (r *Request) SetFormData(data map[string][]string) *Request {
|
|
|
|
|
|
if r.err != nil {
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
if r.doRaw {
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
2026-04-19 15:39:51 +08:00
|
|
|
|
setFormBodyConfig(&r.config.Body, data)
|
|
|
|
|
|
r.invalidatePreparedState()
|
2026-03-08 20:19:40 +08:00
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AddFormData 添加表单数据
|
|
|
|
|
|
func (r *Request) AddFormData(key, value string) *Request {
|
|
|
|
|
|
if r.err != nil {
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
if r.doRaw {
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
2026-04-19 15:39:51 +08:00
|
|
|
|
ensureFormMode(&r.config.Body)
|
2026-03-08 20:19:40 +08:00
|
|
|
|
r.config.Body.FormData[key] = append(r.config.Body.FormData[key], value)
|
2026-04-19 15:39:51 +08:00
|
|
|
|
r.invalidatePreparedState()
|
2026-03-08 20:19:40 +08:00
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AddFormDataMap 批量添加表单数据
|
|
|
|
|
|
func (r *Request) AddFormDataMap(data map[string]string) *Request {
|
|
|
|
|
|
if r.err != nil {
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
if r.doRaw {
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
2026-04-19 15:39:51 +08:00
|
|
|
|
ensureFormMode(&r.config.Body)
|
|
|
|
|
|
for key, value := range data {
|
|
|
|
|
|
r.config.Body.FormData[key] = append(r.config.Body.FormData[key], value)
|
2026-03-08 20:19:40 +08:00
|
|
|
|
}
|
2026-04-19 15:39:51 +08:00
|
|
|
|
r.invalidatePreparedState()
|
2026-03-08 20:19:40 +08:00
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AddFile 添加文件(从路径)
|
|
|
|
|
|
func (r *Request) AddFile(formName, filePath string) *Request {
|
|
|
|
|
|
if r.err != nil {
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
stat, err := os.Stat(filePath)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
r.err = wrapError(ErrFileNotFound, "file: %s", filePath)
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-19 15:39:51 +08:00
|
|
|
|
ensureMultipartMode(&r.config.Body)
|
2026-03-08 20:19:40 +08:00
|
|
|
|
r.config.Body.Files = append(r.config.Body.Files, RequestFile{
|
|
|
|
|
|
FormName: formName,
|
|
|
|
|
|
FileName: stat.Name(),
|
|
|
|
|
|
FilePath: filePath,
|
|
|
|
|
|
FileSize: stat.Size(),
|
|
|
|
|
|
FileType: ContentTypeOctetStream,
|
|
|
|
|
|
})
|
2026-04-19 15:39:51 +08:00
|
|
|
|
r.invalidatePreparedState()
|
2026-03-08 20:19:40 +08:00
|
|
|
|
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AddFileWithName 添加文件(指定文件名)
|
|
|
|
|
|
func (r *Request) AddFileWithName(formName, filePath, fileName string) *Request {
|
|
|
|
|
|
if r.err != nil {
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
stat, err := os.Stat(filePath)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
r.err = wrapError(ErrFileNotFound, "file: %s", filePath)
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-19 15:39:51 +08:00
|
|
|
|
ensureMultipartMode(&r.config.Body)
|
2026-03-08 20:19:40 +08:00
|
|
|
|
r.config.Body.Files = append(r.config.Body.Files, RequestFile{
|
|
|
|
|
|
FormName: formName,
|
|
|
|
|
|
FileName: fileName,
|
|
|
|
|
|
FilePath: filePath,
|
|
|
|
|
|
FileSize: stat.Size(),
|
|
|
|
|
|
FileType: ContentTypeOctetStream,
|
|
|
|
|
|
})
|
2026-04-19 15:39:51 +08:00
|
|
|
|
r.invalidatePreparedState()
|
2026-03-08 20:19:40 +08:00
|
|
|
|
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AddFileWithType 添加文件(指定 MIME 类型)
|
|
|
|
|
|
func (r *Request) AddFileWithType(formName, filePath, fileType string) *Request {
|
|
|
|
|
|
if r.err != nil {
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
stat, err := os.Stat(filePath)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
r.err = wrapError(ErrFileNotFound, "file: %s", filePath)
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-19 15:39:51 +08:00
|
|
|
|
ensureMultipartMode(&r.config.Body)
|
2026-03-08 20:19:40 +08:00
|
|
|
|
r.config.Body.Files = append(r.config.Body.Files, RequestFile{
|
|
|
|
|
|
FormName: formName,
|
|
|
|
|
|
FileName: stat.Name(),
|
|
|
|
|
|
FilePath: filePath,
|
|
|
|
|
|
FileSize: stat.Size(),
|
|
|
|
|
|
FileType: fileType,
|
|
|
|
|
|
})
|
2026-04-19 15:39:51 +08:00
|
|
|
|
r.invalidatePreparedState()
|
2026-03-08 20:19:40 +08:00
|
|
|
|
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AddFileStream 添加文件流
|
|
|
|
|
|
func (r *Request) AddFileStream(formName, fileName string, size int64, reader io.Reader) *Request {
|
|
|
|
|
|
if r.err != nil {
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if reader == nil {
|
|
|
|
|
|
r.err = ErrNilReader
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-19 15:39:51 +08:00
|
|
|
|
ensureMultipartMode(&r.config.Body)
|
2026-03-08 20:19:40 +08:00
|
|
|
|
r.config.Body.Files = append(r.config.Body.Files, RequestFile{
|
|
|
|
|
|
FormName: formName,
|
|
|
|
|
|
FileName: fileName,
|
|
|
|
|
|
FileData: reader,
|
|
|
|
|
|
FileSize: size,
|
|
|
|
|
|
FileType: ContentTypeOctetStream,
|
|
|
|
|
|
})
|
2026-04-19 15:39:51 +08:00
|
|
|
|
r.invalidatePreparedState()
|
2026-03-08 20:19:40 +08:00
|
|
|
|
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AddFileStreamWithType 添加文件流(指定 MIME 类型)
|
|
|
|
|
|
func (r *Request) AddFileStreamWithType(formName, fileName, fileType string, size int64, reader io.Reader) *Request {
|
|
|
|
|
|
if r.err != nil {
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if reader == nil {
|
|
|
|
|
|
r.err = ErrNilReader
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-19 15:39:51 +08:00
|
|
|
|
ensureMultipartMode(&r.config.Body)
|
2026-03-08 20:19:40 +08:00
|
|
|
|
r.config.Body.Files = append(r.config.Body.Files, RequestFile{
|
|
|
|
|
|
FormName: formName,
|
|
|
|
|
|
FileName: fileName,
|
|
|
|
|
|
FileData: reader,
|
|
|
|
|
|
FileSize: size,
|
|
|
|
|
|
FileType: fileType,
|
|
|
|
|
|
})
|
2026-04-19 15:39:51 +08:00
|
|
|
|
r.invalidatePreparedState()
|
2026-03-08 20:19:40 +08:00
|
|
|
|
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|