Skip to content

Instantly share code, notes, and snippets.

@sonya
Last active November 4, 2021 02:45
Show Gist options
  • Save sonya/271b8a1f82d4c6a3a36731ec2d3cfbdc to your computer and use it in GitHub Desktop.
Save sonya/271b8a1f82d4c6a3a36731ec2d3cfbdc to your computer and use it in GitHub Desktop.
http request mocking code samples
type ValueHolder struct {
Value string
}
func GetFixedValue(baseURL string) (string, error) {
url := fmt.Sprintf("%s/fixedvalue", baseURL)
request, _ := http.NewRequest(http.MethodGet, url, nil)
request.Header.Add("Accept", "application/json")
client := &http.Client{}
response, err := client.Do(request)
if response.StatusCode != 200 {
return "", err
}
content, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
v := &ValueHolder{}
err = json.Unmarshal(content, v)
if err != nil {
return "", err
}
return v.Value, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment