Created
October 19, 2019 23:14
-
-
Save glucn/4ee041cc0adb989e906f864d5491821a to your computer and use it in GitHub Desktop.
Decode HTTP Response - version 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"encoding/json" | |
"errors" | |
"net/http" | |
) | |
type Customer struct { | |
ID int `json:"id"` | |
Name string `json:"name"` | |
} | |
func dataFromResponse(r *http.Response) (*Customer, error) { | |
defer r.Body.Close() | |
type Response struct { | |
Customer *Customer `json:"data"` | |
} | |
res := Response{} | |
if err := json.NewDecoder(r.Body).Decode(&res); err != nil { | |
return nil, util.Error(util.Internal, "failed to convert response to Customer: %s", err.Error()) | |
} | |
return res.Customer, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment