Skip to content

Commit

Permalink
Use json.Unmarshal instead of json.Decoder in http.GetData()
Browse files Browse the repository at this point in the history
  • Loading branch information
3cky committed Nov 5, 2019
1 parent 019cdc1 commit 1188e02
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
Expand All @@ -36,7 +38,16 @@ func GetData(url string, res interface{}) error {
}
defer CloseQuietly(r.Body)

return json.NewDecoder(r.Body).Decode(&res)
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}

if r.StatusCode != http.StatusOK {
return fmt.Errorf("HTTP error %d, response:\n%s", r.StatusCode, b)
}

return json.Unmarshal(b, &res)
}

func PostData(url string, data, res interface{}) error {
Expand Down

0 comments on commit 1188e02

Please sign in to comment.