From d0e213b417220b03c27bbdaea4df118eb6f900af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Wed, 11 May 2022 15:11:57 +0800 Subject: [PATCH] update header --- http.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/http.go b/http.go index 4a32a0a..dac32e8 100644 --- a/http.go +++ b/http.go @@ -11,11 +11,14 @@ import ( "io/ioutil" "net/http" "net/url" + "runtime" "strings" "time" ) -const Version = "1.0.17" +const Version = "1.0.18" + +var userAgentFormat = "DtApp-Request/%s (%s) GO/%s" // Response 返回内容 type Response struct { @@ -198,11 +201,20 @@ func request(app *App) (httpResponse Response, err error) { } } + httpResponse.RequestHeader.Set("Sdk-User-Agent", fmt.Sprintf(userAgentFormat, Version, runtime.GOOS, runtime.Version())) + switch app.httpContentType { + case httpParamsModeJson: + httpResponse.RequestHeader.Set("Content-Type", "application/json") + case httpParamsModeForm: + httpResponse.RequestHeader.Set("Content-Type", "application/x-www-form-urlencoded") + case httpParamsModeXml: + httpResponse.RequestHeader.Set("Content-Type", "text/xml") + } + // 请求内容 var reqBody io.Reader if httpResponse.RequestMethod == http.MethodPost && app.httpContentType == httpParamsModeJson { - httpResponse.RequestHeader.Set("Content-Type", "application/json") jsonStr, err := json.Marshal(httpResponse.RequestParams) if err != nil { app.Error = errors.New(fmt.Sprintf("解析出错 %s", err)) @@ -215,7 +227,6 @@ func request(app *App) (httpResponse Response, err error) { if httpResponse.RequestMethod == http.MethodPost && app.httpContentType == httpParamsModeForm { // 携带 form 参数 form := url.Values{} - httpResponse.RequestHeader.Set("Content-Type", "application/x-www-form-urlencoded") if len(httpResponse.RequestParams) > 0 { for k, v := range httpResponse.RequestParams { form.Add(k, GetParamsString(v))