This commit is contained in:
kandrusyak
2023-09-25 21:57:43 +03:00
parent 1fb8c903dc
commit 075fbf89b0
3 changed files with 42 additions and 11 deletions

View File

@@ -3,11 +3,12 @@ package api
import (
"bytes"
"encoding/json"
"github.com/labstack/echo/v4"
"io"
"net/http"
)
func PostRequest(url string, model any, header http.Header) ([]byte, error, *http.Response) {
func PostRequest(url string, model any, header http.Header, c echo.Context) ([]byte, error, *http.Response) {
uJson, err := json.Marshal(model)
if err != nil {
@@ -19,6 +20,13 @@ func PostRequest(url string, model any, header http.Header) ([]byte, error, *htt
req.Header = header
req.Header.Set("Content-Type", "application/json")
debug, err := json.Marshal(req)
if err != nil {
c.Logger().Debug("Request---")
c.Logger().Debug(string(debug))
}
resp, err := client.Do(req)
if err != nil {
@@ -31,10 +39,17 @@ func PostRequest(url string, model any, header http.Header) ([]byte, error, *htt
return nil, err, nil
}
debug, err = json.Marshal(resp)
if err != nil {
c.Logger().Debug("Response---")
c.Logger().Debug(string(debug))
}
return data, nil, resp
}
func GetRequest(url string, header http.Header, args ...string) ([]byte, error, *http.Response) {
func GetRequest(url string, header http.Header, c echo.Context, args ...string) ([]byte, error, *http.Response) {
client := http.Client{}
req, err := http.NewRequest("GET", url, nil)
if len(args) > 0 {
@@ -42,6 +57,14 @@ func GetRequest(url string, header http.Header, args ...string) ([]byte, error,
}
req.Header = header
req.Header.Set("Content-Type", "application/json")
debug, err := json.Marshal(req)
if err != nil {
c.Logger().Debug("Request---")
c.Logger().Debug(string(debug))
}
resp, err := client.Do(req)
if err != nil {
@@ -54,6 +77,11 @@ func GetRequest(url string, header http.Header, args ...string) ([]byte, error,
return nil, err, nil
}
if err != nil {
c.Logger().Debug("Response---")
c.Logger().Debug(string(debug))
}
return data, nil, resp
}