Last active
November 4, 2021 02:45
-
-
Save sonya/271b8a1f82d4c6a3a36731ec2d3cfbdc to your computer and use it in GitHub Desktop.
http request mocking code samples
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
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