Call makes an HTTP request using the provided request object and decodes the response into the specified structure. It automatically sets the request Content-Type to application/json and decodes the JSON response body into the provided response interface. Parameters: - req *http.Request: The prepar
(req *http.Request, response interface{})
| 55 | // - *http.Response: The raw HTTP response object. |
| 56 | // - error: An error if the HTTP request or JSON decoding fails. |
| 57 | func Call(req *http.Request, response interface{}) (*http.Response, error) { |
| 58 | // Set request content type to JSON |
| 59 | req.Header.Set("Content-Type", "application/json") |
| 60 | client := &http.Client{} |
| 61 | |
| 62 | // Send the HTTP request and capture the response |
| 63 | resp, err := client.Do(req) |
| 64 | if err != nil { |
| 65 | return resp, err |
| 66 | } |
| 67 | |
| 68 | // Decode the JSON response into the provided response structure |
| 69 | err = json.NewDecoder(resp.Body).Decode(&response) |
| 70 | if err != nil { |
| 71 | return resp, err |
| 72 | } |
| 73 | return resp, err |
| 74 | } |
| 75 | |
| 76 | // BasicAuth generates a basic HTTP authentication string by encoding the provided username and password. |
| 77 | // The string is base64-encoded in the format "username:password". |