接口请求结果支持gzip解压
This commit is contained in:
parent
fb758a6ad4
commit
024caeea25
33
rpc.go
33
rpc.go
@ -9,6 +9,7 @@ package rpc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@ -334,6 +335,7 @@ func (r *request) Send(ctx *gin.Context, serviceFlag string, apiFlag string, par
|
||||
if !r.httpCodeIsSuccess(response.StatusCode, successHttpCodeList) {
|
||||
return fmt.Errorf("HTTP状态码异常 : %v -> %v", response.StatusCode, response.Status)
|
||||
}
|
||||
|
||||
// 解析响应的业务数据
|
||||
code, message, data = r.getCodeAndMessageAndData(responseBody, responseFieldCfg)
|
||||
if !r.codeIsSuccess(code, successBusinessCodeList) {
|
||||
@ -350,6 +352,37 @@ func (r *request) Send(ctx *gin.Context, serviceFlag string, apiFlag string, par
|
||||
return err
|
||||
}
|
||||
|
||||
// getResponseBody 获取响应体
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 3:10 2022/10/4
|
||||
func (r *request) getResponseBody(response *httpclient.Response) ([]byte, error) {
|
||||
var (
|
||||
responseBody []byte
|
||||
err error
|
||||
gzipInstance *gzip.Reader
|
||||
buf bytes.Buffer
|
||||
)
|
||||
if responseBody, err = io.ReadAll(response.Body); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
// 判断返回值是否经过gzip压缩
|
||||
responseGzip := response.Header.Get("content-encoding")
|
||||
if strings.ToLower(responseGzip) == "gzip" {
|
||||
// 压缩过的数据,在处理response body
|
||||
if gzipInstance, err =gzip.NewReader(bytes.NewReader(responseBody)); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
defer gzipInstance.Close()
|
||||
if _, err = io.Copy(&buf, gzipInstance); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
return responseBody, nil
|
||||
}
|
||||
|
||||
// GetHttpClient 获取client实例
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
|
Loading…
Reference in New Issue
Block a user