Skip to content

Commit

Permalink
fix(auto/http): ptr scoping
Browse files Browse the repository at this point in the history
  • Loading branch information
ezrizhu committed Mar 27, 2023
1 parent ef52fc2 commit adcafed
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions internal/auto/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ import (
func (a *Auto) httpReq(method string, urlStr string, data any) (respBodyBytes []byte, status int, err error) {
c := a.getHttpsClient()

resp := &http.Response{}

switch method {
case "GET":
resp, err = c.Get(urlStr)
resp, err := c.Get(urlStr)
if err != nil {
return nil, -1, err
}
if resp.Body == nil {
return nil, -1, errors.New("response body is nil")
}
defer resp.Body.Close()
respBodyBytes, err = ioutil.ReadAll(resp.Body)
status = resp.StatusCode
case "POST", "PUT", "PATCH":
if data == nil {
return nil, -1, errors.New("data is nil")
Expand All @@ -41,15 +48,16 @@ func (a *Auto) httpReq(method string, urlStr string, data any) (respBodyBytes []
Body: ioutil.NopCloser(bytes.NewBuffer(reqBodyBytes)),
}

resp, err = c.Do(&req)
}

if resp.Body == nil {
return nil, -1, errors.New("response body is nil")
resp, err := c.Do(&req)
if resp.Body == nil {
return nil, -1, errors.New("response body is nil")
}
defer resp.Body.Close()
respBodyBytes, err = ioutil.ReadAll(resp.Body)
status = resp.StatusCode
default:
return nil, -1, errors.New("invalid method")
}
defer resp.Body.Close()

respBodyBytes, err = ioutil.ReadAll(resp.Body)
status = resp.StatusCode
return
}

0 comments on commit adcafed

Please sign in to comment.