diff --git a/server.go b/server.go index 959ce7b..5578566 100644 --- a/server.go +++ b/server.go @@ -791,22 +791,34 @@ func (hc *HttpApiBroker) call(funcname string, w http.ResponseWriter, r *http.Re } } -func CallInternalServiceAPI[T any](url string, apitoken string, method string, data T) error { - reqURL := fmt.Sprintf("%s/api?call=%s", url, method) +func CallInternalServiceAPI[T any](url string, apitoken string, method string, data T, headers ...string) error { + tempHeader := make(http.Header) + tempHeader.Set("MG-X-API-TOKEN", apitoken) + tempHeader.Set("Content-Type", "application/gob") + + for i := 1; i < len(headers); i += 2 { + tempHeader.Set(headers[i-1], headers[i]) + } buff := new(bytes.Buffer) - enc := gob.NewEncoder(buff) - enc.Encode(data) + ct := tempHeader.Get("Content-Type") + if ct == "application/gob" { + enc := gob.NewEncoder(buff) + enc.Encode(data) + } else if ct == "application/json" { + enc := json.NewEncoder(buff) + enc.Encode(data) + } + reqURL := fmt.Sprintf("%s/api?call=%s", url, method) req, err := http.NewRequest("POST", reqURL, buff) if err != nil { return err } - req.Header.Set("MG-X-API-TOKEN", apitoken) - req.Header.Set("Content-Type", "application/gob") - + req.Header = tempHeader resp, err := http.DefaultClient.Do(req) + defer func() { if resp != nil && resp.Body != nil { resp.Body.Close() @@ -822,21 +834,32 @@ func CallInternalServiceAPI[T any](url string, apitoken string, method string, d return err } -func CallInternalServiceAPIAs[Tin any, Tout any](url string, apitoken string, method string, data Tin, out *Tout) error { - reqURL := fmt.Sprintf("%s/api?call=%s", url, method) +func CallInternalServiceAPIAs[Tin any, Tout any](url string, apitoken string, method string, data Tin, out *Tout, headers ...string) error { + tempHeader := make(http.Header) + tempHeader.Set("MG-X-API-TOKEN", apitoken) + tempHeader.Set("Content-Type", "application/gob") + + for i := 1; i < len(headers); i += 2 { + tempHeader.Set(headers[i-1], headers[i]) + } buff := new(bytes.Buffer) - enc := gob.NewEncoder(buff) - enc.Encode(data) + ct := tempHeader.Get("Content-Type") + if ct == "application/gob" { + enc := gob.NewEncoder(buff) + enc.Encode(data) + } else if ct == "application/json" { + enc := json.NewEncoder(buff) + enc.Encode(data) + } + reqURL := fmt.Sprintf("%s/api?call=%s", url, method) req, err := http.NewRequest("POST", reqURL, buff) if err != nil { return err } - req.Header.Set("MG-X-API-TOKEN", apitoken) - req.Header.Set("Content-Type", "application/gob") - + req.Header = tempHeader resp, err := http.DefaultClient.Do(req) if err != nil { return err