优化body设置
This commit is contained in:
parent
ede74d9508
commit
049f9cca4f
16
rpc.go
16
rpc.go
@ -16,7 +16,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
|
|
||||||
@ -236,7 +235,13 @@ func (r *request) Send(ctx *gin.Context, serviceFlag string, apiFlag string, par
|
|||||||
|
|
||||||
// 完整的请求地址
|
// 完整的请求地址
|
||||||
fullURL, body = r.getFullURLAndBody(serviceConfig, apiConfig, parameter)
|
fullURL, body = r.getFullURLAndBody(serviceConfig, apiConfig, parameter)
|
||||||
if response, err = client.Do(apiConfig.Method, fullURL, apiConfig.Header, bytes.NewReader(body)); nil != err {
|
// 获取客户端
|
||||||
|
client = r.GetHttpClient(apiConfig.Header, apiConfig.Timeout)
|
||||||
|
var bodyReader io.Reader
|
||||||
|
if nil != body {
|
||||||
|
bodyReader = bytes.NewReader(body)
|
||||||
|
}
|
||||||
|
if response, err = client.Do(apiConfig.Method, fullURL, apiConfig.Header, bodyReader); nil != err {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,8 +275,8 @@ func (r *request) GetHttpClient(header map[string]string, timeout ApiTimeout) *h
|
|||||||
timeout.Read = DefaultReadTimeout
|
timeout.Read = DefaultReadTimeout
|
||||||
}
|
}
|
||||||
client.WithHeaders(header)
|
client.WithHeaders(header)
|
||||||
client.WithOption(httpclient.OPT_CONNECTTIMEOUT_MS, time.Duration(timeout.Connect)*time.Millisecond)
|
client.WithOption(httpclient.OPT_CONNECTTIMEOUT_MS, timeout.Connect)
|
||||||
client.WithOption(httpclient.OPT_TIMEOUT_MS, time.Duration(timeout.Read)*time.Millisecond)
|
client.WithOption(httpclient.OPT_TIMEOUT_MS, timeout.Read)
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -311,9 +316,6 @@ func (r *request) getFullURLAndBody(serviceConfig *Service, apiConfig *Api, para
|
|||||||
case http.MethodDelete:
|
case http.MethodDelete:
|
||||||
body, _ = json.Marshal(parameter)
|
body, _ = json.Marshal(parameter)
|
||||||
}
|
}
|
||||||
if nil == body {
|
|
||||||
body = []byte("{}")
|
|
||||||
}
|
|
||||||
query := strings.Join(parameterPair, "&")
|
query := strings.Join(parameterPair, "&")
|
||||||
if len(query) == 0 {
|
if len(query) == 0 {
|
||||||
return fullURL, body
|
return fullURL, body
|
||||||
|
Loading…
x
Reference in New Issue
Block a user