Skip to content

Instantly share code, notes, and snippets.

@arindamroynitw
Last active August 15, 2019 17:43
Show Gist options
  • Save arindamroynitw/7d0f640578bd55a83bff17eeeb005b38 to your computer and use it in GitHub Desktop.
Save arindamroynitw/7d0f640578bd55a83bff17eeeb005b38 to your computer and use it in GitHub Desktop.
Function to get response from mock server - Timeouts in Golang
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