40 lines
783 B
Go
40 lines
783 B
Go
|
package misc
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
type HeaderType struct {
|
||
|
Key string
|
||
|
Value string
|
||
|
}
|
||
|
|
||
|
func Fetch(method string, url string, body io.Reader, headers []HeaderType) ([]byte, error) {
|
||
|
req, err := http.NewRequest(method, url, body)
|
||
|
Logger.Info().Msg(
|
||
|
fmt.Sprintf("'%s' request for '%s'", method, url),
|
||
|
)
|
||
|
|
||
|
if err != nil {
|
||
|
return []byte{}, fmt.Errorf("error creating request: %v", err)
|
||
|
}
|
||
|
|
||
|
req.Header.Set("Content-Type", "application/json")
|
||
|
if len(headers) > 0 {
|
||
|
for _, header := range headers {
|
||
|
req.Header.Set(header.Key, header.Value)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
client := &http.Client{}
|
||
|
response, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
return []byte{}, fmt.Errorf("error sending request: %v", err)
|
||
|
}
|
||
|
defer response.Body.Close()
|
||
|
|
||
|
return io.ReadAll(response.Body)
|
||
|
}
|