From 10aee4db7403e50cd60921a0dad39a1aaf841fcd Mon Sep 17 00:00:00 2001 From: Prajith04 Date: Fri, 20 Mar 2026 10:59:04 +0530 Subject: [PATCH] added RequestBytes function to return byte array for go kit --- pkg/client/client.go | 22 ++++++++++++++++++++++ pkg/client/common.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/pkg/client/client.go b/pkg/client/client.go index 693ffc2..a930fdf 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -53,3 +53,25 @@ func (c *Client) Request( } return request(ctx, c.httpClient, opts) } +func (c *Client) RequestBytes( + ctx context.Context, + method string, + baseURL string, + endpoint string, + body io.Reader, + params map[string]string) ([]byte,error){ + queryParams := url.Values{} + for key, value := range params { + queryParams.Set(key, value) + } + opts := &requestOptions{ + method: method, + baseURL: baseURL, + endpoint: endpoint, + authString: c.authHeader, + payload: body, + queryParams: queryParams, + } + return requestBytes(ctx, c.httpClient, opts) + + } diff --git a/pkg/client/common.go b/pkg/client/common.go index 5adc77e..7673c4f 100644 --- a/pkg/client/common.go +++ b/pkg/client/common.go @@ -58,5 +58,43 @@ func request( if err != nil { return "", err } + return formattedJSON.String(), nil } + +func requestBytes( + ctx context.Context, + client httpClient, + opts *requestOptions) ([]byte, error) { + + req, err := http.NewRequestWithContext(ctx, opts.method, opts.baseURL, opts.payload) + if err != nil { + return nil, err + } + req.Header.Add("Authorization", opts.authString) + req.Header.Add("Content-Type", `application/json`) + req.URL.Path += opts.endpoint + + if opts.queryParams != nil { + req.URL.RawQuery = opts.queryParams.Encode() + } + + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + var formattedJSON bytes.Buffer + err = json.Indent(&formattedJSON, body, "", "\t") + if err != nil { + return nil, err + } + + return formattedJSON.Bytes(), nil +}