Documentation
¶
Overview ¶
Package response writes standardized JSON success and error envelopes.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SendError ¶
SendError writes a JSON error response. The status code defaults to 400 unless an optional code argument is provided.
func SendSuccess ¶
SendSuccess writes a JSON success response. The status code defaults to 200 unless an optional code argument is provided.
Example ¶
SendSuccess wraps the payload in the standard JSON envelope ({code, message, data, status}) and writes it with a 200 status.
package main
import (
"fmt"
"io"
"net/http/httptest"
"github.com/gofiber/fiber/v2"
"github.com/rahmadafandi/fibr/response"
)
func main() {
app := fiber.New()
app.Get("/ping", func(c *fiber.Ctx) error {
return response.SendSuccess(c, fiber.Map{"pong": true}, "ok")
})
resp, _ := app.Test(httptest.NewRequest("GET", "/ping", nil))
body, _ := io.ReadAll(resp.Body)
fmt.Println("status:", resp.StatusCode)
fmt.Println("body:", string(body))
}
Output: status: 200 body: {"code":200,"message":"ok","data":{"pong":true},"status":"success"}
Types ¶
type Response ¶
type Response struct {
Code int `json:"code"`
Message string `json:"message"`
Error string `json:"error,omitempty"` // machine-readable code; set on errors only
Data any `json:"data,omitempty"`
Status string `json:"status"`
}
Response is the standard JSON envelope returned by all API handlers.
Click to show internal directories.
Click to hide internal directories.