Fiber
01 / 02

Routing, Context & Body Parsing

Routing, Context & Body Parsing

Basic Server

package main

import "github.com/gofiber/fiber/v2"

func getUser(c *fiber.Ctx) error {
  id := c.Params("id")
  return c.JSON(fiber.Map{"id": id})
}

func main() {
  app := fiber.New()
  app.Get("/users/:id", getUser)
  app.Listen(":8080")
}

Fiber's API deliberately mirrors Express.js (app.Get(), c.JSON()) to ease the transition for developers coming from Node.js. It's built on fasthttp rather than Go's standard net/http — a different HTTP engine than Echo/Gin use.

Body Parsing & Query Params

type CreateUserRequest struct {
  Name  string `json:"name"`
  Email string `json:"email"`
}

func createUser(c *fiber.Ctx) error {
  req := new(CreateUserRequest)
  if err := c.BodyParser(req); err != nil {
    return fiber.NewError(fiber.StatusBadRequest, "invalid body")
  }
  return c.Status(fiber.StatusCreated).JSON(req)
}

// c.Params("id") -> path param, c.Query("page") -> query string — distinct methods

Route Groups

api := app.Group("/api/v1")
api.Use(authMiddleware)
api.Get("/users/:id", getUser)
api.Post("/users", createUser)

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free