imroc / req

Simple Go HTTP client with Black Magic
https://req.cool
MIT License
4.12k stars 334 forks source link

解gzip之后的render会panic #322

Open 223225zzzkkk opened 5 months ago

223225zzzkkk commented 5 months ago

代码: reader, := gzip.NewReader(response.Response.Body)//body是经过gzip压缩的json all, := io.ReadAll(reader)//这里会panic

报错:panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x280 pc=0xa6ef62]

goroutine 1 [running]: compress/gzip.(*Reader).Read(0xffffffffffffffff?, {0xc00021e000?, 0x0?, 0x0?}) C:/Program Files/Go/src/compress/gzip/gunzip.go:247 +0x22 io.ReadAll({0xd7fd80, 0x0}) C:/Program Files/Go/src/io/io.go:704 +0x7e main.main() C:/Users/khuj/GolandProjects/charts1/main.go:57 +0xa7 ps:官方的http能正常解压缩...

imroc commented 5 months ago

如果要自己解压缩,可以显式设置Accept-Encoding为 gzip,并设置不自动读取 body:

client := req.C().SetCommonHeader("Accept-Encoding", "gzip").DisableAutoReadResponse()
223225zzzkkk commented 5 months ago

好的,我不知道有自动解码,因为我在创建客户端时显示设置了Accept-Encoding。 感谢