Dieses Kapitel stellt vor, wie Go Fiber Anfragen und Antworten behandelt, in der Regel durch die Rückgabe von JSON-Daten für die aktuelle API-Entwicklung.
Das Fiber-Framework verwendet hauptsächlich die Rückgabedaten des Ctx-Kontextobjekts.
Rückgabe von JSON-Daten
type SomeStruct struct {
  Name string
  Age  uint8
}
app.Get("/json", func(c *fiber.Ctx) error {
  // Datenstruktur für die Antwort erstellen
  data := SomeStruct{
    Name: "Grame",
    Age:  20,
  }
  // Rückgabe unter Verwendung der json-Funktion
  return c.JSON(data)
  // => Content-Type: application/json
  // => "{"Name": "Grame", "Age": 20}"
  return c.JSON(fiber.Map{
    "name": "Grame",
    "age": 20,
  })
  // => Content-Type: application/json
  // => "{"name": "Grame", "age": 20}"
})
Rückgabe im JSONP-Format
type SomeStruct struct {
  name string
  age  uint8
}
app.Get("/", func(c *fiber.Ctx) error {
  // Datenstruktur erstellen
  data := SomeStruct{
    name: "Grame",
    age:  20,
  }
  return c.JSONP(data)
  // => callback({"name": "Grame", "age": 20})
  return c.JSONP(data, "customFunc")
  // => customFunc({"name": "Grame", "age": 20})
})
Rückgabe von Text
app.Get("/", func(c *fiber.Ctx) error {
        // Gib "Hallo, Welt ?!" als Zeichenkette zurück
        return c.SendString("Hallo, Welt ?!")
})
Rückgabe von Statuscode
app.Get("/not-found", func(c *fiber.Ctx) error {
  // Setze den Antwortstatuscode
  return c.SendStatus(415)
  // => 415 "Unzulässiger Medientyp"
  c.SendString("Hallo, Welt!")
  // Setze den Antwortstatuscode
  return c.SendStatus(415)
  // => 415 "Hallo, Welt!"
})
Rückgabe von Dateien (Implementieren von Dateidownload)
app.Get("/", func(c *fiber.Ctx) error {
  return c.Download("./files/report-12345.pdf");
  // => Bericht-12345.pdf herunterladen
  return c.Download("./files/report-12345.pdf", "report.pdf");
  // => Bericht.pdf herunterladen
})
Festlegung von Anforderungsheadern
app.Get("/", func(c *fiber.Ctx) error {
  c.Set("Content-Type", "text/plain")
  // => "Content-Type: text/plain"
  // ...
})
Setzen von Cookies
Cookie-Definition
type Cookie struct {
    Name        string    `json:"name"`
    Value       string    `json:"value"`
    Path        string    `json:"path"`
    Domain      string    `json:"domain"`
    MaxAge      int       `json:"max_age"`
    Expires     time.Time `json:"expires"`
    Secure      bool      `json:"secure"`
    HTTPOnly    bool      `json:"http_only"`
    SameSite    string    `json:"same_site"`
    SessionOnly bool      `json:"session_only"`
}
Rückgabe des Cookies
app.Get("/", func(c *fiber.Ctx) error {
  // Cookie erstellen
  cookie := new(fiber.Cookie)
  cookie.Name = "john"
  cookie.Value = "doe"
  cookie.Expires = time.Now().Add(24 * time.Hour)
  // Cookie setzen
  c.Cookie(cookie)
  // ...
})
Weiterleitung (301 oder 302)
Standardmäßig wird eine 302-Weiterleitung durchgeführt
app.Get("/", func(c *fiber.Ctx) error {
  return c.Redirect("/foo/bar")
  return c.Redirect("../login")
  return c.Redirect("http://example.com")
  return c.Redirect("http://example.com", 301)
})