- use post if url is too long for jdapi.

This commit is contained in:
gazebo
2018-10-29 19:26:06 +08:00
parent a0c6fe75d8
commit e9d0a172ee

View File

@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"sort" "sort"
"strings"
"git.rosy.net.cn/baseapi" "git.rosy.net.cn/baseapi"
@@ -137,21 +138,35 @@ func (a *API) AccessAPI(apiStr string, jdParams map[string]interface{}) (retVal
if jdParams == nil { if jdParams == nil {
jdParams = make(map[string]interface{}, 0) jdParams = make(map[string]interface{}, 0)
} }
jdParamStr, err := json.Marshal(jdParams) jdParamBytes, err := json.Marshal(jdParams)
if err != nil { if err != nil {
baseapi.SugarLogger.Errorf("Error when marshal %v, error:%v", jdParams, err) baseapi.SugarLogger.Errorf("Error when marshal %v, error:%v", jdParams, err)
return nil, err return nil, err
} }
params["jd_param_json"] = string(jdParamStr) jdParamStr := string(jdParamBytes)
userGet := true
if len(jdParamStr) > 12 {
userGet = false
}
params["jd_param_json"] = jdParamStr
params["timestamp"] = utils.GetCurTimeStr() params["timestamp"] = utils.GetCurTimeStr()
sign := a.signParams(params) sign := a.signParams(params)
params[signKey] = sign params[signKey] = sign
err = platformapi.AccessPlatformAPIWithRetry(a.client, err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request { func() *http.Request {
fullURL := utils.GenerateGetURL(prodURL, apiStr, params) var request *http.Request
// baseapi.SugarLogger.Debug(fullURL) if userGet {
request, _ := http.NewRequest(http.MethodGet, fullURL, nil) fullURL := utils.GenerateGetURL(prodURL, apiStr, params)
// baseapi.SugarLogger.Debug(fullURL)
request, _ = http.NewRequest(http.MethodGet, fullURL, nil)
} else {
fullURL := prodURL + "/" + apiStr
// baseapi.SugarLogger.Debug(utils.Map2URLValues(params).Encode())
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(params).Encode()))
request.Header.Set("charset", "UTF-8")
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
// request.Close = true //todo 为了性能考虑还是不要关闭 // request.Close = true //todo 为了性能考虑还是不要关闭
return request return request
}, },
@@ -338,7 +353,9 @@ func (a *API) AccessAPIHavePage(apiStr string, jdParams map[string]interface{},
} }
func forceInnerCode2Str(innerCode interface{}) string { func forceInnerCode2Str(innerCode interface{}) string {
if innerCodeStr, ok := innerCode.(string); ok { if innerCode == nil {
return "0"
} else if innerCodeStr, ok := innerCode.(string); ok {
return innerCodeStr return innerCodeStr
} }
return fmt.Sprintf("%v", innerCode) return fmt.Sprintf("%v", innerCode)