Last active
August 15, 2019 17:43
-
-
Save arindamroynitw/7d0f640578bd55a83bff17eeeb005b38 to your computer and use it in GitHub Desktop.
Function to get response from mock server - Timeouts in Golang
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" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
) | |
func GetHttpResponse() (*Response, error) { | |
resp, err := http.Get("https://jsonplaceholder.typicode.com/todos/1") | |
if err != nil { | |
return nil, fmt.Errorf("error in http call") | |
} | |
defer resp.Body.Close() | |
byteResp, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
return nil, fmt.Errorf("error in reading response") | |
} | |
structResp := &Response{} | |
err = json.Unmarshal(byteResp, structResp) | |
if err != nil { | |
return nil, fmt.Errorf("error in unmarshalling response") | |
} | |
return structResp, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment