mastodon-markdown-archive/client/api.go

33 lines
503 B
Go
Raw Normal View History

2024-05-18 14:08:07 +00:00
package client
import (
"encoding/json"
"io"
"net/http"
)
2024-05-19 20:57:30 +00:00
func Fetch(requestUrl string, variable interface{}, headers map[string]string) error {
client := &http.Client{}
req, _ := http.NewRequest("GET", requestUrl, nil)
for key, val := range headers {
req.Header.Set(key, val)
}
res, err := client.Do(req)
2024-05-18 14:08:07 +00:00
if err != nil {
return err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err := json.Unmarshal(body, variable); err != nil {
return err
}
return nil
}