response

package
v2.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 1 Imported by: 0

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

func SendError(c fiber.Ctx, data any, message string, code ...int) error

SendError writes a JSON error response. The status code defaults to 400 unless an optional code argument is provided.

func SendSuccess

func SendSuccess(c fiber.Ctx, data any, message string, code ...int) error

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/v3"
	"github.com/rahmadafandi/fibr/v2/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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL