diff --git a/pkg/gofr/handler.go b/pkg/gofr/handler.go index 32a363cf3..c6077b81e 100644 --- a/pkg/gofr/handler.go +++ b/pkg/gofr/handler.go @@ -99,8 +99,6 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Handle custom headers if 'result' is a 'Response'. if resp, ok := result.(response.Response); ok { resp.SetCustomHeaders(w) - - result = resp.Data } // Handler function completed diff --git a/pkg/gofr/http/responder.go b/pkg/gofr/http/responder.go index 668a55a3f..0072b151a 100644 --- a/pkg/gofr/http/responder.go +++ b/pkg/gofr/http/responder.go @@ -41,7 +41,12 @@ func (r Responder) Respond(data interface{}, err error) { data = nil } - resp = response{Data: data, Error: errorObj} + res, ok := data.(resTypes.Response) + if !ok { + resp = response{Data: data, Error: errorObj} + } else { + resp = res + } } r.w.Header().Set("Content-Type", "application/json") @@ -91,8 +96,9 @@ func createErrorResponse(err error) map[string]interface{} { // response represents an HTTP response. type response struct { - Error interface{} `json:"error,omitempty"` - Data interface{} `json:"data,omitempty"` + Error interface{} `json:"error,omitempty"` + Metadata interface{} `json:"metadata,omitempty"` + Data interface{} `json:"data,omitempty"` } type statusCodeResponder interface { diff --git a/pkg/gofr/http/response/response.go b/pkg/gofr/http/response/response.go index 702e513e4..f1c83a205 100644 --- a/pkg/gofr/http/response/response.go +++ b/pkg/gofr/http/response/response.go @@ -5,8 +5,9 @@ import ( ) type Response struct { - Data any `json:"data"` - Headers map[string]string `json:"-"` + Data any `json:"data"` + Metadata map[string]any `json:"metadata,omitempty"` + Headers map[string]string `json:"-"` } func (resp Response) SetCustomHeaders(w http.ResponseWriter) {