From fc2121efc71525f0ee3708396cdfe96aed3bc533 Mon Sep 17 00:00:00 2001 From: Yun Date: Sat, 24 Feb 2024 10:15:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0curl=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E8=BF=94=E5=9B=9Eresp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- curlx.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/curlx.go b/curlx.go index 6c1fc14..cf8907b 100644 --- a/curlx.go +++ b/curlx.go @@ -155,6 +155,43 @@ func (c *Curlx) Send(ctx context.Context, p ...Param) (res []byte, httpcode int, return body, status, nil } +func (c *Curlx) SendWithResponee(ctx context.Context, ps ...Param) Response { + r := Response{} + req, resp, err := c.SendExec(ctx, ps...) + r.req = req + r.resp = resp + + if err != nil { + r.err = err + return r + } + var body []byte + switch resp.Header.Get("Content-Encoding") { + case "gzip": + reader, err := gzip.NewReader(resp.Body) + if err != nil { + r.err = err + return r + } + for { + buf := make([]byte, 1024) + n, err := reader.Read(buf) + if err != nil && err != io.EOF { + panic(err) + } + if n == 0 { + break + } + body = append(body, buf...) + } + default: + body, _ = io.ReadAll(resp.Body) + } + r.body = body + c.opts.Logger.Infof(ctx, "curlx.Send body:%s", string(body)) + return r +} + /** * 执行发送 * 注意:外部使用需要加这一句 defer response.Body.Close()