Skip to main content
The Ctx interface represents the HTTP request and response context. It provides methods for accessing request data, sending responses, and managing the request lifecycle.

Type Definition

type Handler = func(Ctx) error

Core Methods

App

Returns the App instance.
func (c Ctx) App() *App
*App
*App
Returns the Fiber app reference.
Example
app.Get("/", func(c fiber.Ctx) error {
    config := c.App().Config()
    return c.JSON(config)
})

Context

Returns a standard context.Context.
func (c Ctx) Context() context.Context
context.Context
context.Context
Returns a context.Context instance safe for async operations.
Example
app.Get("/", func(c fiber.Ctx) error {
    ctx := c.Context()
    go doAsyncWork(ctx)
    return nil
})

SetContext

Set a custom context.Context.
func (c Ctx) SetContext(ctx context.Context)
ctx
context.Context
The context to associate with the request.

Request Methods

Params

Get route parameters.
func (c Ctx) Params(key string, defaultValue ...string) string
key
string
The parameter name.
defaultValue
string
Default value if parameter doesn’t exist.
string
string
Returns the parameter value.
Example
app.Get("/user/:id", func(c fiber.Ctx) error {
    id := c.Params("id")
    return c.SendString("User: " + id)
})

Query

Get query string parameters.
func (c Ctx) Query(key string, defaultValue ...string) string
key
string
The query parameter name.
defaultValue
string
Default value if parameter doesn’t exist.
string
string
Returns the query parameter value.
Example
// GET /search?q=fiber
app.Get("/search", func(c fiber.Ctx) error {
    q := c.Query("q")
    return c.SendString("Query: " + q)
})

Queries

Get all query parameters as a map.
func (c Ctx) Queries() map[string]string
map[string]string
map[string]string
Returns all query parameters.

Body

Get the request body (with decompression).
func (c Ctx) Body() []byte
[]byte
[]byte
Returns the decompressed request body.
Example
app.Post("/", func(c fiber.Ctx) error {
    body := c.Body()
    return c.Send(body)
})

BodyRaw

Get the raw request body (without decompression).
func (c Ctx) BodyRaw() []byte
[]byte
[]byte
Returns the raw request body.

Bind

Get the Bind helper for request parsing.
func (c Ctx) Bind() *Bind
*Bind
*Bind
Returns the Bind helper instance.
Example
type User struct {
    Name  string `json:"name"`
    Email string `json:"email"`
}

app.Post("/user", func(c fiber.Ctx) error {
    user := new(User)
    if err := c.Bind().Body(user); err != nil {
        return err
    }
    return c.JSON(user)
})

Headers

Get

Get a request header value.
func (c Ctx) Get(key string, defaultValue ...string) string
key
string
The header name (case-insensitive).
defaultValue
string
Default value if header doesn’t exist.
string
string
Returns the header value.
Example
app.Get("/", func(c fiber.Ctx) error {
    contentType := c.Get("Content-Type")
    return c.SendString(contentType)
})

GetReqHeaders

Get all request headers.
func (c Ctx) GetReqHeaders() map[string][]string
map[string][]string
map[string][]string
Returns all request headers.

Cookies

Get a cookie value.
func (c Ctx) Cookies(key string, defaultValue ...string) string
key
string
The cookie name.
defaultValue
string
Default value if cookie doesn’t exist.
string
string
Returns the cookie value.

Request Information

Method

Get or override the HTTP method.
func (c Ctx) Method(override ...string) string
override
string
Optional method to override.
string
string
Returns the HTTP method.

Path

Get or override the request path.
func (c Ctx) Path(override ...string) string
override
string
Optional path to override.
string
string
Returns the request path.

Protocol

Get the protocol (http or https).
func (c Ctx) Protocol() string
string
string
Returns “http” or “https”.

Secure

Check if the connection is TLS.
func (c Ctx) Secure() bool
bool
bool
Returns true if TLS connection.

IP

Get the remote IP address.
func (c Ctx) IP() string
string
string
Returns the client IP address.

IPs

Get IP addresses from X-Forwarded-For header.
func (c Ctx) IPs() []string
[]string
[]string
Returns array of IP addresses.

Host

Get the host with port.
func (c Ctx) Host() string
string
string
Returns the hostname with port.

Hostname

Get the hostname without port.
func (c Ctx) Hostname() string
string
string
Returns the hostname.

Port

Get the remote port.
func (c Ctx) Port() string
string
string
Returns the port number.

BaseURL

Get the base URL (protocol + host).
func (c Ctx) BaseURL() string
string
string
Returns the base URL.

OriginalURL

Get the original request URL.
func (c Ctx) OriginalURL() string
string
string
Returns the original URL with query string.

FullURL

Get the full request URL.
func (c Ctx) FullURL() string
string
string
Returns the complete URL.

Response Methods

Status

Set the response status code.
func (c Ctx) Status(status int) Ctx
status
int
The HTTP status code.
Ctx
Ctx
Returns the context for chaining.
Example
app.Get("/", func(c fiber.Ctx) error {
    return c.Status(fiber.StatusOK).SendString("OK")
})

SendString

Send a string response.
func (c Ctx) SendString(s string) error
s
string
The string to send.
error
error
Returns an error if send fails.

SendStatus

Send only a status code.
func (c Ctx) SendStatus(status int) error
status
int
The HTTP status code.
error
error
Returns an error if send fails.

JSON

Send a JSON response.
func (c Ctx) JSON(data any) error
data
any
The data to serialize as JSON.
error
error
Returns an error if serialization or send fails.
Example
app.Get("/api/user", func(c fiber.Ctx) error {
    return c.JSON(fiber.Map{
        "name": "John",
        "age":  25,
    })
})

XML

Send an XML response.
func (c Ctx) XML(data any) error
data
any
The data to serialize as XML.
error
error
Returns an error if serialization or send fails.

Send

Send a byte response.
func (c Ctx) Send(b []byte) error
b
[]byte
The bytes to send.
error
error
Returns an error if send fails.

SendFile

Send a file as response.
func (c Ctx) SendFile(path string, compress ...bool) error
path
string
The file path to send.
compress
bool
Enable compression (default: true).
error
error
Returns an error if file send fails.

Redirect

Get the redirect helper.
func (c Ctx) Redirect() *Redirect
*Redirect
*Redirect
Returns the Redirect helper instance.
Example
app.Get("/old", func(c fiber.Ctx) error {
    return c.Redirect().To("/new")
})

Headers & Cookies (Response)

Set

Set a response header.
func (c Ctx) Set(key, value string)
key
string
The header name.
value
string
The header value.

Append

Append a value to a response header.
func (c Ctx) Append(key, value string)
key
string
The header name.
value
string
The value to append.
Set a cookie.
func (c Ctx) Cookie(cookie *Cookie)
The cookie to set.
Example
app.Get("/", func(c fiber.Ctx) error {
    c.Cookie(&fiber.Cookie{
        Name:  "session",
        Value: "abc123",
        MaxAge: 3600,
    })
    return c.SendString("Cookie set")
})

ClearCookie

Clear a cookie.
func (c Ctx) ClearCookie(key ...string)
key
...string
Cookie names to clear. If omitted, clears all cookies.

Route & Locals

Route

Get the current route.
func (c Ctx) Route() *Route
*Route
*Route
Returns the matched route.

Locals

Store or retrieve local values.
func (c Ctx) Locals(key any, value ...any) any
key
any
The key to store/retrieve.
value
any
The value to store.
any
any
Returns the stored value.
Example
app.Use(func(c fiber.Ctx) error {
    c.Locals("user", "john")
    return c.Next()
})

app.Get("/", func(c fiber.Ctx) error {
    user := c.Locals("user").(string)
    return c.SendString("Hello " + user)
})

Control Flow

Next

Execute the next handler in the stack.
func (c Ctx) Next() error
error
error
Returns an error from the next handler.
Example
app.Use(func(c fiber.Ctx) error {
    fmt.Println("Before")
    err := c.Next()
    fmt.Println("After")
    return err
})

RestartRouting

Restart routing from the first handler.
func (c Ctx) RestartRouting() error
error
error
Returns an error if routing fails.

Content Negotiation

Accepts

Check if the client accepts a content type.
func (c Ctx) Accepts(offers ...string) string
offers
...string
Content types or extensions to check.
string
string
Returns the best match or empty string.

AcceptsJSON

Check if the client accepts JSON.
func (c Ctx) AcceptsJSON() bool
bool
bool
Returns true if JSON is accepted.

AcceptsHTML

Check if the client accepts HTML.
func (c Ctx) AcceptsHTML() bool
bool
bool
Returns true if HTML is accepted.

AcceptsXML

Check if the client accepts XML.
func (c Ctx) AcceptsXML() bool
bool
bool
Returns true if XML is accepted.

Utilities

Is

Check the Content-Type.
func (c Ctx) Is(extension string) bool
extension
string
The content type or extension to check.
bool
bool
Returns true if content type matches.

IsJSON

Check if Content-Type is JSON.
func (c Ctx) IsJSON() bool
bool
bool
Returns true if JSON.

IsForm

Check if Content-Type is form-encoded.
func (c Ctx) IsForm() bool
bool
bool
Returns true if form-encoded.

Fresh

Check if the response is fresh.
func (c Ctx) Fresh() bool
bool
bool
Returns true if response is still fresh.

Stale

Check if the response is stale.
func (c Ctx) Stale() bool
bool
bool
Returns true if response is stale.

XHR

Check if request is XMLHttpRequest.
func (c Ctx) XHR() bool
bool
bool
Returns true if XHR request.

Build docs developers (and LLMs) love