🧠
Context
The Ctx struct represents the Context which hold the HTTP request and response. It has methods for the request query string, parameters, body, HTTP headers, and so on.

Accepts

Checks, if the specified extensions or content types are acceptable.
Based on the request’s Accept HTTP header.
Signature
1
func (c *Ctx) Accepts(offers ...string) string
2
func (c *Ctx) AcceptsCharsets(offers ...string) string
3
func (c *Ctx) AcceptsEncodings(offers ...string) string
4
func (c *Ctx) AcceptsLanguages(offers ...string) string
Copied!
Example
1
// Accept: text/*, application/json
2
​
3
app.Get("/", func(c *fiber.Ctx) error {
4
c.Accepts("html") // "html"
5
c.Accepts("text/html") // "text/html"
6
c.Accepts("json", "text") // "json"
7
c.Accepts("application/json") // "application/json"
8
c.Accepts("image/png") // ""
9
c.Accepts("png") // ""
10
// ...
11
})
Copied!
Fiber provides similar functions for the other accept headers.
1
// Accept-Charset: utf-8, iso-8859-1;q=0.2
2
// Accept-Encoding: gzip, compress;q=0.2
3
// Accept-Language: en;q=0.8, nl, ru
4
​
5
app.Get("/", func(c *fiber.Ctx) error {
6
c.AcceptsCharsets("utf-16", "iso-8859-1")
7
// "iso-8859-1"
8
​
9
c.AcceptsEncodings("compress", "br")
10
// "compress"
11
​
12
c.AcceptsLanguages("pt", "nl", "ru")
13
// "nl"
14
// ...
15
})
Copied!

AllParams

Params is used to get all route parameters. Using Params method to get params.
Signature
1
func (c *Ctx) AllParams() map[string]string
Copied!
Example
1
// GET http://example.com/user/fenny
2
app.Get("/user/:name", func(c *fiber.Ctx) error {
3
c.AllParams() // "{"name": "fenny"}"
4
​
5
// ...
6
})
7
​
8
// GET http://example.com/user/fenny/123
9
app.Get("/user/*", func(c *fiber.Ctx) error {
10
c.AllParams() // "{"*1": "fenny/123"}"
11
​
12
// ...
13
})
Copied!

App

Returns the *App reference so you could easily access all application settings.
Signature
1
func (c *Ctx) App() *App
Copied!
Example
1
app.Get("/stack", func(c *fiber.Ctx) error {
2
return c.JSON(c.App().Stack())
3
})
Copied!

Append

Appends the specified value to the HTTP response header field.
If the header is not already set, it creates the header with the specified value.
Signature
1
func (c *Ctx) Append(field string, values ...string)
Copied!
Example
1
app.Get("/", func(c *fiber.Ctx) error {
2
c.Append("Link", "http://google.com", "http://localhost")
3
// => Link: http://localhost, http://google.com
4
​
5
c.Append("Link", "Test")
6
// => Link: http://localhost, http://google.com, Test
7
​
8
// ...
9
})
Copied!

Attachment

Sets the HTTP response Content-Disposition header field to attachment.
Signature
1
func (c *Ctx) Attachment(filename ...string)
Copied!
Example
1
app.Get("/", func(c *fiber.Ctx) error {
2
c.Attachment()
3
// => Content-Disposition: attachment
4
​
5
c.Attachment("./upload/images/logo.png")
6
// => Content-Disposition: attachment; filename="logo.png"
7
// => Content-Type: image/png
8
​
9
// ...
10
})
Copied!

BaseURL

Returns the base URL (protocol + host) as a string.
Signature
1
func (c *Ctx) BaseURL() string
Copied!
Example
1
// GET https://example.com/page#chapter-1
2
​
3
app.Get("/", func(c *fiber.Ctx) error {
4
c.BaseURL() // https://example.com
5
// ...
6
})
Copied!

Bind

Add vars to default view var map binding to template engine. Variables are read by the Render method and may be overwritten.
Signature
1
func (c *Ctx) Bind(vars Map) error
Copied!
Example
1
app.Use(func(c *fiber.Ctx) error {
2
c.Bind(fiber.Map{
3
"Title": "Hello, World!",
4
})
5
})
6
​
7
app.Get("/", func(c *fiber.Ctx) error {
8
return c.Render("xxx.tmpl", fiber.Map{}) // Render will use Title variable
9
})
Copied!

Body

Returns the raw request body.
Signature
1
func (c *Ctx) Body() []byte
Copied!
Example
1
// curl -X POST http://localhost:8080 -d user=john
2
​
3
app.Post("/", func(c *fiber.Ctx) error {
4
// Get raw body from POST request:
5
return c.Send(c.Body()) // []byte("user=john")
6
})
Copied!
Returned value is only valid within the handler. Do not store any references. Make copies or use the Immutable setting instead. Read more...​

BodyParser

Binds the request body to a struct.
It is important to specify the correct struct tag based on the content type to be parsed. For example, if you want to parse a JSON body with a field called Pass, you would use a struct field of json:"pass".
content-type
struct tag
application/x-www-form-urlencoded
form
multipart/form-data
form
application/json
json
application/xml
xml
text/xml
xml
Signature
1
func (c *Ctx) BodyParser(out interface{}) error
Copied!
Example
1
// Field names should start with an uppercase letter
2
type Person struct {
3
Name string `json:"name" xml:"name" form:"name"`
4
Pass string `json:"pass" xml:"pass" form:"pass"`
5
}
6
​
7
app.Post("/", func(c *fiber.Ctx) error {
8
p := new(Person)
9
​
10
if err := c.BodyParser(p); err != nil {
11
return err
12
}
13
​
14
log.Println(p.Name) // john
15
log.Println(p.Pass) // doe
16
​
17
// ...
18
})
19
​
20
// Run tests with the following curl commands
21
​
22
// curl -X POST -H "Content-Type: application/json" --data "{\"name\":\"john\",\"pass\":\"doe\"}" localhost:3000
23
​
24
// curl -X POST -H "Content-Type: application/xml" --data "<login><name>john</name><pass>doe</pass></login>" localhost:3000
25
​
26
// curl -X POST -H "Content-Type: application/x-www-form-urlencoded" --data "name=john&pass=doe" localhost:3000
27
​
28
// curl -X POST -F name=john -F pass=doe http://localhost:3000
29
​
30
// curl -X POST "http://localhost:3000/?name=john&pass=doe"
Copied!
Returned value is only valid within the handler. Do not store any references. Make copies or use the Immutable setting instead. Read more...​

ClearCookie

Expire a client cookie (or all cookies if left empty)
Signature
1
func (c *Ctx) ClearCookie(key ...string)
Copied!
Example
1
app.Get("/", func(c *fiber.Ctx) error {
2
// Clears all cookies:
3
c.ClearCookie()
4
​
5
// Expire specific cookie by name:
6
c.ClearCookie("user")
7
​
8
// Expire multiple cookies by names:
9
c.ClearCookie("token", "session", "track_id", "version")
10
// ...
11
})
Copied!
Web browsers and other compliant clients will only clear the cookie if the given options are identical to those when creating the cookie, excluding expires and maxAge. ClearCookie will not set these values for you - a technique similar to the one shown below should be used to ensure your cookie is deleted.
Example
1
app.Get("/set", func(c *fiber.Ctx) error {
2
c.Cookie(&fiber.Cookie{
3
Name: "token",
4
Value: "randomvalue",
5
Expires: time.Now().Add(24 * time.Hour),
6
HTTPOnly: true,
7
SameSite: "lax",
8
})
9
​
10
// ...
11
})
12
​
13
app.Get("/delete", func(c *fiber.Ctx) error {
14
c.Cookie(&fiber.Cookie{
15
Name: "token",
16
// Set expiry date to the past
17
Expires: time.Now().Add(-(time.Hour * 2)),
18
HTTPOnly: true,
19
SameSite: "lax",
20
})
21
​
22
// ...
23
})
Copied!

Context

Returns *fasthttp.RequestCtx that is compatible with the context.Context interface that requires a deadline, a cancellation signal, and other values across API boundaries.
Signature
1
func (c *Ctx) Context() *fasthttp.RequestCtx
Copied!
Please read the Fasthttp Documentation for more information.

Cookie

Set cookie
Signature
1
func (c *Ctx) Cookie(cookie *Cookie)
Copied!
1
type Cookie struct {
2
Name string `json:"name"`
3
Value string `json:"value"`
4
Path string `json:"path"`
5
Domain string `json:"domain"`
6
MaxAge int `json:"max_age"`
7
Expires time.Time `json:"expires"`
8
Secure bool `json:"secure"`
9
HTTPOnly bool `json:"http_only"`
10
SameSite string `json:"same_site"`
11
SessionOnly bool `json:"session_only"`
12
}
Copied!
Example
1
app.Get("/", func(c *fiber.Ctx) error {
2
// Create cookie
3
cookie := new(fiber.Cookie)
4
cookie.Name = "john"
5
cookie.Value = "doe"
6
cookie.Expires = time.Now().Add(24 * time.Hour)
7
​
8
// Set cookie
9
c.Cookie(cookie)
10
// ...
11
})
Copied!

Cookies

Get cookie value by key, you could pass an optional default value that will be returned if the cookie key does not exist.
Signatures
1
func (c *Ctx) Cookies(key string, defaultValue ...string) string
Copied!
Example
1
app.Get("/", func(c *fiber.Ctx) error {
2
// Get cookie by key:
3
c.Cookies("name") // "john"
4
c.Cookies("empty", "doe") // "doe"
5
// ...
6
})
Copied!
Returned value is only valid within the handler. Do not store any references. Make copies or use the Immutable setting instead. Read more...​

Download

Transfers the file from path as an attachment.
Typically, browsers will prompt the user to download. By default, the Content-Disposition header filename= parameter is the file path (this typically appears in the browser dialog).
Override this default with the filename parameter.
Signature
1
func (c *Ctx) Download(file string, filename ...string) error
Copied!
Example
1
app.Get("/", func(c *fiber.Ctx) error {
2
return c.Download("./files/report-12345.pdf");
3
// => Download report-12345.pdf
4
​
5
return c.Download("./files/report-12345.pdf", "report.pdf");
6
// => Download report.pdf
7
})
Copied!

Format

Performs content-negotiation on the Accept HTTP header. It uses Accepts to select a proper format.
If the header is not specified or there is no proper format, text/plain is used.
Signature
1
func (c *Ctx) Format(body interface{}) error
Copied!
Example
1
app.Get("/", func(c *fiber.Ctx) error {
2
// Accept: text/plain
3
c.Format("Hello, World!")
4
// => Hello, World!
5
​
6
// Accept: text/html
7
c.Format("Hello, World!")
8
// => <p>Hello, World!</p>
9
​
10
// Accept: application/json
11
c.Format("Hello, World!")
12
// => "Hello, World!"
13
// ..
14
})
Copied!

FormFile

MultipartForm files can be retrieved by name, the first file from the given key is returned.
Signature
1
func (c *Ctx) FormFile(key string) (*multipart.FileHeader, error)
Copied!
Example
1
app.Post("/", func(c *fiber.Ctx) error {
2
// Get first file from form field "document":
3
file, err := c.FormFile("document")
4
​
5
// Save file to root directory:
6
return c.SaveFile(file, fmt.Sprintf("./%s", file.Filename))
7
})
Copied!

FormValue

Any form values can be retrieved by name, the first value from the given key is returned.
Signature
1
func (c *Ctx) FormValue(key string, defaultValue ...string) string
Copied!
Example
1
app.Post("/", func(c *fiber.Ctx) error {
2
// Get first value from form field "name":
3
c.FormValue("name")
4
// => "john" or "" if not exist
5
​
6
// ..
7
})
Copied!
Returned value is only valid within the handler. Do not store any references. Make copies or use the Immutable setting instead. Read more...​

Fresh

Signature
1
func (c *Ctx) Fresh() bool
Copied!

Get

Returns the HTTP request header specified by the field.
The match is case-insensitive.
Signature
1
func (c *Ctx) Get(key string, defaultValue ...string) string
Copied!
Example
1
app.Get("/", func(c *fiber.Ctx) error {
2
c.Get("Content-Type") // "text/plain"
3
c.Get("CoNtEnT-TypE") // "text/plain"
4
c.Get("something", "john") // "john"
5
// ..
6
})
Copied!
Returned value is only valid within the handler. Do not store any references. Make copies or use the Immutable setting instead. Read more...​

GetReqHeaders

Returns the HTTP request headers.
Signature
1
func (c *Ctx) GetReqHeaders() map[string]string
Copied!

GetRespHeader

Returns the HTTP response header specified by the field.
The match is case-insensitive.
Signature
1
func (c *Ctx) GetRespHeader(key string, defaultValue ...string) string
Copied!
Example
1
app.Get("/", func(c *fiber.Ctx) error {
2
c.GetRespHeader("X-Request-Id") // "8d7ad5e3-aaf3-450b-a241-2beb887efd54"
3
c.GetRespHeader("Content-Type") // "text/plain"
4
c.GetRespHeader("something", "john") // "john"
5
// ..
6
})
Copied!
Returned value is only valid within the handler. Do not store any references. Make copies or use the Immutable setting instead. Read more...​

GetRespHeaders

Returns the HTTP response headers.
Signature
1
func (c *Ctx) GetRespHeaders() map[string]string
Copied!

GetRouteURL

Generates URLs to named routes, with parameters. URLs are relative, for example: "/user/1831"
Signature
1
func (c *Ctx) GetRouteURL(routeName string, params Map) (string, error)
Copied!
Example
1
app.Get("/", func(c *fiber.Ctx) error {
2
return c.SendString("Home page")
3
}).Name("home")
4
​
5
app.Get("/user/:id", func(c *fiber.Ctx) error {
6
return c.SendString(c.Params("id"))
7
}).Name("user.show")
8
​
9
app.Get("/test", func(c *fiber.Ctx) error {
10
location, _ := c.GetRouteURL("user.show", fiber.Map{"id": 1})
11
return c.SendString(location)
12
})
13
​
14
// /test returns "/user/1"
Copied!

Hostname

Returns the hostname derived from the Host HTTP header.
Signature
1
func (c *Ctx) Hostname() string
Copied!
Example
1
// GET http://google.com/search
2
​
3
app.Get("/", func(c *fiber.Ctx) error {
4
c.Hostname() // "google.com"
5
​
6
// ...
7
})
Copied!
Returned value is only valid within the handler. Do not store any references. Make copies or use the Immutable setting instead. Read more...​

IP

Returns the remote IP address of the request.
Signature
1
func (c *Ctx) IP() string
Copied!
Example
1
app.Get("/", func(c *fiber.Ctx) error {
2
c.IP() // "127.0.0.1"
3
​
4
// ...
5
})
Copied!

IPs

Returns an array of IP addresses specified in the X-Forwarded-For request header.
Signature
1
func (c *Ctx) IPs() []string
Copied!
Example
1
// X-Forwarded-For: proxy1, 127.0.0.1, proxy3
2
​
3
app.Get("/", func(c *fiber.Ctx) error {
4
c.IPs() // ["proxy1", "127.0.0.1", "proxy3"]
5
​
6
// ...
7
})
Copied!

Is

Returns the matching content type, if the incoming request’s Content-Type HTTP header field matches the MIME type specified by the type parameter.
If the request has no body, it returns false.
Signature
1
func (c *Ctx) Is(extension string) bool
Copied!
Example
1
// Content-Type: text/html; charset=utf-8
2
​
3
app.Get("/", func(c *fiber.Ctx) error {
4
c.Is("html") // true
5
c.Is(".html") // true
6
c.Is("json") // false
7
​
8
// ...
9
})
Copied!

IsFromLocal

Returns true if request came from localhost
Signature
1
func (c *Ctx) IPs() []string
Copied!
Example
1
app.Get("/", func(c *fiber.Ctx) error {
2
// If request came from localhost, return true else return false
3
c.isFromLocal()
4
​
5
// ...
6
})
Copied!

JSON

Converts any interface or string to JSON using the goccy/go-json package.
JSON also sets the content header to application/json.
Signature
1
func (c *Ctx) JSON(data interface{}) error
Copied!
Example
1
type SomeStruct struct {
2
Name string
3
Age uint8
4
}
5
​
6
app.Get("/json", func(c *fiber.Ctx) error {
7
// Create data struct:
8
data := SomeStruct{
9
Name: "Grame",
10
Age: 20,
11
}
12
​
13
return c.JSON(data)
14
// => Content-Type: application/json
15
// => "{"Name": "Grame", "Age": 20}"
16
​
17
return c.JSON(fiber.Map{
18
"name": "Grame",
19
"age": 20,
20
})
21
// => Content-Type: application/json
22
// => "{"name": "Grame", "age": 20}"
23
})
Copied!

JSONP

Sends a JSON response with JSONP support. This method is identical to JSON, except that it opts-in to JSONP callback support. By default, the callback name is simply callback.
Override this by passing a named string in the method.
Signature
1
func (c *Ctx) JSONP(data interface{}, callback ...string) error
Copied!
Example
1
type SomeStruct struct {
2
name string
3
age uint8
4
}
5
​
6
app.Get("/", func(c *fiber.Ctx) error {
7
// Create data struct:
8
data := SomeStruct{
9
name: "Grame",
10
age: 20,
11
}
12
​
13
return c.JSONP(data)
14
// => callback({"name": "Grame", "age": 20})
15
​
16
return c.JSONP(data, "customFunc")
17
// => customFunc({"name": "Grame", "age": 20})
18
})
Copied!

Links

Joins the links followed by the property to populate the response’s Link HTTP header field.
Signature
1
func (c *Ctx) Links(link ...string)
Copied!
Example
1
app.Get("/", func(c *fiber.Ctx) error {
2
c.Links(
3
"http://api.example.com/users?page=2", "next",
4
"http://api.example.com/users?page=5", "last",
5
)
6
// Link: <http://api.example.com/users?page=2>; rel="next",
7
// <http://api.example.com/users?page=5>; rel="last"
8
​
9
// ...
10
})
Copied!

Locals

A method that stores variables scoped to the request and, therefore, are available only to the routes that match the request.
This is useful if you want to pass some specific data to the next middleware.
Signature
1
func (c *Ctx) Locals(key string, value ...interface{}) interface{}
Copied!
Example
1
app.Use(func(c *fiber.Ctx) error {
2
c.Locals("user", "admin")
3
return c.Next()
4
})
5
​
6
app.Get("/admin", func(c *fiber.Ctx) error {
7
if c.Locals("user") == "admin" {
8
return c.Status(fiber.StatusOK).SendString("Welcome, admin!")
9
}
10
return c.SendStatus(fiber.StatusForbidden)
11
​
12
})
Copied!

Location

Sets the response Location HTTP header to the specified path parameter.
Signature
1
func (c *Ctx) Location(path string)
Copied!
Example
1
app.Post("/", func(c *fiber.Ctx) error {
2
return c.Location("http://example.com")
3
​
4
return c.Location("/foo/bar")
5
})
Copied!

Method

Returns a string corresponding to the HTTP method of the request: GET, POST, PUT, and so on. Optionally, you could override the method by passing a string.
Signature
1
func (c *Ctx) Method(override ...string) string
Copied!
Example
1
app.Post("/", func(c *fiber.Ctx) error {
2
c.Method() // "POST"
3
​
4
c.Method("GET")
5
c.Method() // GET
6
​
7
// ...
8
})
Copied!

MultipartForm

To access multipart form entries, you can parse the binary with MultipartForm(). This returns a map[string][]string, so given a key, the value will be a string slice.
Signature
1
func (c *Ctx) MultipartForm() (*multipart.Form, error)
Copied!
Example
1
app.Post("/", func(c *fiber.Ctx) error {
2
// Parse the multipart form:
3
if form, err := c.MultipartForm(); err == nil {
4
// => *multipart.Form
5
​
6
if token := form.Value["token"]; len(token) > 0 {
7
// Get key value:
8
fmt.Println(token[0])
9
}
10
​
11
// Get all files from "documents" key:
12
files := form.File["documents"]
13
// => []*multipart.FileHeader
14
​
15
// Loop through files:
16
for _, file := range files {
17
fmt.Println(file.Filename, file.Size, file.Header["Content-Type"][0])
18
// => "tutorial.pdf" 360641 "application/pdf"
19
​
20
// Save the files to disk:
21
if err := c.SaveFile(file, fmt.Sprintf("./%s", file.Filename)); err != nil {
22
return err
23
}
24
}
25
}
26
​
27
return err
28
})
Copied!

Next

When Next is called, it executes the next method in the stack that matches the current route. You can pass an error struct within the method that will end the chaining and call the error handler.
Signature
1
func (c *Ctx) Next() error
Copied!
Example
1
app.Get("/", func(c *fiber.Ctx) error {
2
fmt.Println("1st route!")
3
return c.Next()
4
})
5
​
6
app.Get("*", func(c *fiber.Ctx) error {
7
fmt.Println("2nd route!")
8
return c.Next()
9
})
10
​
11
app.Get("/", func(c *fiber.Ctx) error {
12
fmt.Println("3rd route!")
13
return c.SendString("Hello, World!")
14
})
Copied!

OriginalURL

Returns the original request URL.
Signature
1
func (c *Ctx) OriginalURL() string
Copied!
Example
1
// GET http://example.com/search?q=something
2
​
3
app.Get("/", func(c *fiber.Ctx) error {
4
c.OriginalURL() // "/search?q=something"
5
​
6
// ...
7
})
Copied!
Returned value is only valid within the handler. Do not store any references. Make copies or use the Immutable setting instead. Read more...​

Params

Method can be used to get the route parameters, you could pass an optional default value that will be returned if the param key does not exist.
Defaults to empty string (""), if the param doesn't exist.
Signature
1
func (c *Ctx) Params(key string, defaultValue ...string) string
Copied!
Example
1
// GET http://example.com/user/fenny
2
app.Get("/user/:name", func(c *fiber.Ctx) error {
3
c.Params("name") // "fenny"
4
​
5
// ...
6
})
7
​
8
// GET http://example.com/user/fenny/123
9
app.Get("/user/*", func(c *fiber.Ctx) error {
10
c.Params("*") // "fenny/123"
11
c.Params("*1") // "fenny/123"
12
​
13
// ...
14
})
Copied!
Unnamed route parameters(*, +) can be fetched by the character and the counter in the route.
Example
1
// ROUTE: /v1/*/shop/*
2
// GET: /v1/brand/4/shop/blue/xs
3
c.Params("*1") // "brand/4"
4
c.Params("*2") // "blue/xs"
Copied!
For reasons of downward compatibility, the first parameter segment for the parameter character can also be accessed without the counter.
Example
1
app.Get("/v1/*/shop/*", func(c *fiber.Ctx) error {
2
c.Params("*") // outputs the values of the first wildcard segment
3
})
Copied!
Returned value is only valid within the handler. Do not store any references. Make copies or use the Immutable setting instead. Read more...​

ParamsInt

Method can be used to get an integer from the route parameters. Please note if that parameter is not in the request, zero will be returned. If the parameter is NOT a number, zero and an error will be returned
Defaults to empty string (""), if the param doesn't exist.
Signature
1
func (c *Ctx) Params(key string) (int, error)
Copied!
Example
1
// GET http://example.com/user/123
2
app.Get("/user/:id", func(c *fiber.Ctx) error {
3
id, err := c.ParamsInt("id") // int 123 and no error
4
​
5
// ...
6
})
Copied!
This method is equivalent of using atoi with ctx.Params

Path

Contains the path part of the request URL. Optionally, you could override the path by passing a string. For internal redirects, you might want to call RestartRouting instead of Next.