fix issue of nil response handling for ghttp.Client

This commit is contained in:
John Guo 2021-02-01 21:51:42 +08:00
parent 8368e11827
commit ce40d139e7
4 changed files with 7 additions and 2 deletions

1
go.mod
View File

@ -10,6 +10,7 @@ require (
github.com/gomodule/redigo v2.0.0+incompatible
github.com/gorilla/websocket v1.4.1
github.com/grokify/html-strip-tags-go v0.0.0-20190921062105-daaa06bf1aaf
github.com/mattn/go-runewidth v0.0.10 // indirect
github.com/olekukonko/tablewriter v0.0.1
go.opentelemetry.io/otel v0.16.0
golang.org/x/net v0.0.0-20201031054903-ff519b6c9102

View File

@ -58,7 +58,7 @@ func (r *Response) RawRequest() string {
// RawResponse returns the raw content of the response.
func (r *Response) RawResponse() string {
// Response can be nil.
// Response might be nil.
if r == nil || r.Response == nil {
return ""
}

View File

@ -117,7 +117,7 @@ func (c *Client) DoRequest(method, url string, data ...interface{}) (resp *Respo
}
// Auto saving cookie content.
if c.browserMode && resp != nil {
if c.browserMode && resp != nil && resp.Response != nil {
now := time.Now()
for _, v := range resp.Response.Cookies() {
if !v.Expires.IsZero() && v.Expires.UnixNano() < now.UnixNano() {

View File

@ -49,6 +49,10 @@ func (r *Response) GetCookieMap() map[string]string {
// ReadAll retrieves and returns the response content as []byte.
func (r *Response) ReadAll() []byte {
// Response might be nil.
if r == nil || r.Response == nil {
return []byte{}
}
body, err := ioutil.ReadAll(r.Response.Body)
if err != nil {
return nil