Accepts

Checks, if the specified extensions or content types are acceptable.

Based on the request’s Accept HTTP header.

  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
  1. // Accept: text/*, application/json
  2. app.Get("/", func(c *fiber.Ctx) error {
  3. c.Accepts("html") // "html"
  4. c.Accepts("text/html") // "text/html"
  5. c.Accepts("json", "text") // "json"
  6. c.Accepts("application/json") // "application/json"
  7. c.Accepts("image/png") // ""
  8. c.Accepts("png") // ""
  9. // ...
  10. })

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. app.Get("/", func(c *fiber.Ctx) error {
  5. c.AcceptsCharsets("utf-16", "iso-8859-1")
  6. // "iso-8859-1"
  7. c.AcceptsEncodings("compress", "br")
  8. // "compress"
  9. c.AcceptsLanguages("pt", "nl", "ru")
  10. // "nl"
  11. // ...
  12. })