RPC请求结果支持对编码进行处理
This commit is contained in:
parent
739a50167e
commit
cc32decb80
10
go.mod
10
go.mod
@ -3,17 +3,19 @@ module git.zhangdeman.cn/zhangdeman/rpc
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220702052429-b5ae4cf80989
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20221010125206-1c9c8be53c82
|
||||
github.com/ddliu/go-httpclient v0.6.9
|
||||
github.com/gin-gonic/gin v1.8.1
|
||||
github.com/tidwall/gjson v1.14.1
|
||||
github.com/tidwall/gjson v1.14.3
|
||||
go.uber.org/zap v1.21.0
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/Jeffail/gabs v1.4.0 // indirect
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-ini/ini v1.66.6 // indirect
|
||||
github.com/go-ini/ini v1.67.0 // indirect
|
||||
github.com/go-playground/locales v0.14.0 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.0 // indirect
|
||||
github.com/go-playground/validator/v10 v10.11.0 // indirect
|
||||
@ -27,7 +29,7 @@ require (
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.0 // indirect
|
||||
github.com/tidwall/pretty v1.2.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.7 // indirect
|
||||
go.uber.org/atomic v1.9.0 // indirect
|
||||
go.uber.org/multierr v1.8.0 // indirect
|
||||
|
12
go.sum
12
go.sum
@ -1,5 +1,11 @@
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220702052429-b5ae4cf80989 h1:2yuM4QDfQ2UjzxNdtCY7CpOxclp31BqWQg09mB1ydfk=
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220702052429-b5ae4cf80989/go.mod h1:aQsgOqtMnnwG2n7T/0a1OkYigqBzZivv8Ea6683bK2M=
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20221010125206-1c9c8be53c82 h1:N9s9fAFWzFNrq7GPdcW8hSG1quRw3S/w+ypwe+nG8xo=
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20221010125206-1c9c8be53c82/go.mod h1:zTir/0IWdK3E7n0GiaogyWHADAQnBtTdl2I6Z2/OPqw=
|
||||
github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo=
|
||||
github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
|
||||
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
|
||||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
@ -14,6 +20,8 @@ github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
|
||||
github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk=
|
||||
github.com/go-ini/ini v1.66.6 h1:h6k2Bb0HWS/BXXHCXj4QHjxPmlIU4NK+7MuLp9SD+4k=
|
||||
github.com/go-ini/ini v1.66.6/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
||||
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
|
||||
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
|
||||
@ -72,10 +80,14 @@ github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8
|
||||
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
|
||||
github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo=
|
||||
github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw=
|
||||
github.com/tidwall/gjson v1.14.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
|
||||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
|
||||
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo=
|
||||
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
|
||||
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
|
||||
|
22
rpc.go
22
rpc.go
@ -360,9 +360,9 @@ func (r *request) Send(ctx *gin.Context, serviceFlag string, apiFlag string, par
|
||||
func (r *request) getResponseBody(response *httpclient.Response) ([]byte, error) {
|
||||
var (
|
||||
responseBody []byte
|
||||
err error
|
||||
err error
|
||||
gzipInstance *gzip.Reader
|
||||
buf bytes.Buffer
|
||||
buf bytes.Buffer
|
||||
)
|
||||
if responseBody, err = io.ReadAll(response.Body); nil != err {
|
||||
return nil, err
|
||||
@ -371,14 +371,26 @@ func (r *request) getResponseBody(response *httpclient.Response) ([]byte, error)
|
||||
responseGzip := response.Header.Get("content-encoding")
|
||||
if strings.ToLower(responseGzip) == "gzip" {
|
||||
// 压缩过的数据,在处理response body
|
||||
if gzipInstance, err =gzip.NewReader(bytes.NewReader(responseBody)); nil != err {
|
||||
if gzipInstance, err = gzip.NewReader(bytes.NewReader(responseBody)); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
defer gzipInstance.Close()
|
||||
defer func() {
|
||||
_ = gzipInstance.Close()
|
||||
}()
|
||||
if _, err = io.Copy(&buf, gzipInstance); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
responseBody = buf.Bytes()
|
||||
}
|
||||
// 判断是否为gbk编码,若是,转为utf8
|
||||
isGBK := strings.Contains(strings.ToLower(response.Header.Get("content-type")), "gbk")
|
||||
if isGBK {
|
||||
return []byte(util.String.Convert(string(responseBody), "gbk", "utf-8")), nil
|
||||
}
|
||||
// 判断是否为gb2312,若是,转为utf8
|
||||
isGB2312 := strings.Contains(strings.ToLower(response.Header.Get("content-type")), "gb2312")
|
||||
if isGB2312 {
|
||||
return []byte(util.String.Convert(string(responseBody), "gb2312", "utf-8")), nil
|
||||
}
|
||||
return responseBody, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user