优化body设置

This commit is contained in:
白茶清欢 2022-07-01 16:44:12 +08:00
parent ede74d9508
commit 049f9cca4f

16
rpc.go
View File

@ -16,7 +16,6 @@ import (
"net/http"
"strings"
"sync"
"time"
"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)
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
}
@ -270,8 +275,8 @@ func (r *request) GetHttpClient(header map[string]string, timeout ApiTimeout) *h
timeout.Read = DefaultReadTimeout
}
client.WithHeaders(header)
client.WithOption(httpclient.OPT_CONNECTTIMEOUT_MS, time.Duration(timeout.Connect)*time.Millisecond)
client.WithOption(httpclient.OPT_TIMEOUT_MS, time.Duration(timeout.Read)*time.Millisecond)
client.WithOption(httpclient.OPT_CONNECTTIMEOUT_MS, timeout.Connect)
client.WithOption(httpclient.OPT_TIMEOUT_MS, timeout.Read)
return client
}
@ -311,9 +316,6 @@ func (r *request) getFullURLAndBody(serviceConfig *Service, apiConfig *Api, para
case http.MethodDelete:
body, _ = json.Marshal(parameter)
}
if nil == body {
body = []byte("{}")
}
query := strings.Join(parameterPair, "&")
if len(query) == 0 {
return fullURL, body