add header filter

This commit is contained in:
kandrusyak
2023-06-23 18:05:01 +03:00
parent eab7ec0c90
commit 7d2aaf40e4
5 changed files with 64 additions and 11 deletions

View File

@@ -7,15 +7,39 @@ import (
"net/http"
)
func PostRequest(url string, model any) ([]byte, error) {
func PostRequest(url string, model any, header http.Header) ([]byte, error) {
uJson, err := json.Marshal(model)
if err != nil {
return nil, err
}
resp, err := http.Post(url,
"application/json", bytes.NewReader(uJson))
client := http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewReader(uJson))
req.Header = header
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return data, nil
}
func GetRequest(url string, header http.Header) ([]byte, error) {
client := http.Client{}
req, err := http.NewRequest("GET", url, nil)
req.Header = header
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err