Skip to main content
gFly v1.18.1
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage
Edit page

Responses

Note
IMPORTANT!
The response file should be placed in directory internal/http/response

How to response error, string, HTML, JSON, download, View data

Response error

// Simple 
return errors.NotImplemented

// Structure JSON
return c.Error(response.Error{
    Code:    "BAD_REQUEST",
    Message: "Invalid JWT token",
}, core.StatusUnauthorized)

Response string, HTML

// String
return c.String("Hello world")

// HTML
return c.HTML("<h2>Hello world</h2>")

Response JSON

return c.Status(gfly.StatusOK).JSON(response.SignIn{
    Access:  tokens.Access,
    Refresh: tokens.Refresh,
})

Response download

return c.Download("./storage/logs/logs.log", "Log_file.log")

Response View

func (m *UploadFormPage) Handle(c *gfly.Ctx) error {
    return c.View("upload", gfly.ViewData{})
}

View template resources/views/upload.tpl

Response Data Types for APIs

The framework provides standard response structures located in internal/http/response:

Success Responses

Meta - Pagination metadata structure

// Meta struct to describe pagination metadata information.
// @Description Contains pagination metadata including current page, items per page, and total count
type Meta struct {
    Page    int `json:"page,omitempty" example:"1" doc:"Current page number"`
    PerPage int `json:"per_page,omitempty" example:"10" doc:"Number of items per page"`
    Total   int `json:"total" example:"1354" doc:"Total number of records"`
}

List[T] - Generic list response with pagination

// List struct to describe a generic list response.
// @Description Generic list response structure
type List[T any] struct {
    Meta Meta `json:"meta" example:"{\"page\":1,\"per_page\":10,\"total\":100}" doc:"Metadata information for pagination"`
    Data []T  `json:"data" example:"[]" doc:"List of category data"`
}

Success - Generic success response

// Success struct to describe a generic success response.
// @Description Generic success response structure
type Success struct {
    Message string    `json:"message" example:"Operation completed successfully"`  // Success message description
    Data    core.Data `json:"data" doc:"Additional data related to the operation"` // Optional data related to the success operation
}

Error Responses

Error - Generic error response structure

// Error struct to describe login response.
// @Description Generic error response structure
type Error struct {
    Code    string    `json:"code" example:"BAD_REQUEST"`    // Error code
    Message string    `json:"message" example:"Bad request"` // Error message description
    Data    core.Data `json:"data"`                          // Useful for validation's errors
}