This commit is contained in:
gospider
2025-03-27 17:41:39 +08:00
parent 2e524cb97e
commit 9da7a11bee
3 changed files with 26 additions and 25 deletions

27
body.go
View File

@@ -199,33 +199,44 @@ func (obj *OrderData) MarshalJSON() ([]byte, error) {
}
return buf.Bytes(), nil
}
func (obj *RequestOption) newBody(val any) (io.Reader, *OrderData, bool, error) {
// type bodyT = int
// const (
// bodyTypeNone bodyT = iota
// bodyTypeForm
// bodyTypeJson
// bodyTypeText
// bodyTypeParams
// )
func (obj *RequestOption) newBody(val any) (io.Reader, *OrderData, error) {
switch value := val.(type) {
case *OrderData:
return nil, value, true, nil
return nil, value, nil
case io.Reader:
obj.readOne = true
return value, nil, true, nil
return value, nil, nil
case string:
return bytes.NewReader(tools.StringToBytes(value)), nil, true, nil
return bytes.NewReader(tools.StringToBytes(value)), nil, nil
case []byte:
return bytes.NewReader(value), nil, true, nil
return bytes.NewReader(value), nil, nil
case map[string]any:
orderMap := NewOrderData()
for key, val := range value {
orderMap.Add(key, val)
}
return nil, orderMap, true, nil
return nil, orderMap, nil
default:
jsonData, err := gson.Decode(val)
if err != nil {
return nil, nil, false, errors.New("invalid body type")
return nil, nil, errors.New("invalid body type")
}
orderMap := NewOrderData()
for kk, vv := range jsonData.Map() {
orderMap.Add(kk, vv.Value())
}
return nil, orderMap, false, nil
return nil, orderMap, nil
}
}

View File

@@ -5,7 +5,6 @@ import (
"context"
"crypto/rand"
"crypto/tls"
"errors"
"fmt"
"io"
"net/url"
@@ -115,7 +114,7 @@ func randomBoundary() string {
func (obj *RequestOption) initBody(ctx context.Context) (io.Reader, error) {
if obj.Body != nil {
body, orderData, _, err := obj.newBody(obj.Body)
body, orderData, err := obj.newBody(obj.Body)
if err != nil {
return nil, err
}
@@ -131,13 +130,10 @@ func (obj *RequestOption) initBody(ctx context.Context) (io.Reader, error) {
if obj.ContentType == "" {
obj.ContentType = randomBoundary()
}
body, orderData, ok, err := obj.newBody(obj.Form)
body, orderData, err := obj.newBody(obj.Form)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.New("not support type")
}
if body != nil {
return body, nil
}
@@ -151,13 +147,10 @@ func (obj *RequestOption) initBody(ctx context.Context) (io.Reader, error) {
if obj.ContentType == "" {
obj.ContentType = "application/x-www-form-urlencoded"
}
body, orderData, ok, err := obj.newBody(obj.Data)
body, orderData, err := obj.newBody(obj.Data)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.New("not support type")
}
if body != nil {
return body, nil
}
@@ -166,7 +159,7 @@ func (obj *RequestOption) initBody(ctx context.Context) (io.Reader, error) {
if obj.ContentType == "" {
obj.ContentType = "application/json"
}
body, orderData, _, err := obj.newBody(obj.Json)
body, orderData, err := obj.newBody(obj.Json)
if err != nil {
return nil, err
}
@@ -178,7 +171,7 @@ func (obj *RequestOption) initBody(ctx context.Context) (io.Reader, error) {
if obj.ContentType == "" {
obj.ContentType = "text/plain"
}
body, orderData, _, err := obj.newBody(obj.Text)
body, orderData, err := obj.newBody(obj.Text)
if err != nil {
return nil, err
}
@@ -195,13 +188,10 @@ func (obj *RequestOption) initParams() (*url.URL, error) {
if obj.Params == nil {
return baseUrl, nil
}
body, dataData, ok, err := obj.newBody(obj.Params)
body, dataData, err := obj.newBody(obj.Params)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.New("not support type")
}
var query string
if body != nil {
paramsBytes, err := io.ReadAll(body)

View File

@@ -219,7 +219,7 @@ func (obj *Client) request(ctx *Response) (err error) {
}
}
}
if ctx.Request().Body != nil {
if ctx.Request() != nil && ctx.Request().Body != nil {
ctx.Request().Body.Close()
}
}()