Questo capitolo introduce come Go Fiber gestisce la richiesta e la risposta, tipicamente restituendo dati JSON per lo sviluppo corrente delle API.

Il framework Fiber utilizza principalmente i dati di ritorno dell'oggetto contestuale Ctx.

Restituzione dei dati JSON

type SomeStruct struct {
  Name string
  Age  uint8
}

app.Get("/json", func(c *fiber.Ctx) error {
  // Creare la struttura dati per la risposta
  data := SomeStruct{
    Name: "Grame",
    Age:  20,
  }

  // Restituire utilizzando la funzione json
  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}"
})

Restituzione del formato JSONP

type SomeStruct struct {
  name string
  age  uint8
}

app.Get("/", func(c *fiber.Ctx) error {
  // Creare la struttura dati
  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})
})

Restituzione del testo

app.Get("/", func(c *fiber.Ctx) error {
        // Restituire "Ciao, Mondo ?!" come stringa
        return c.SendString("Ciao, Mondo ?!")
})

Restituzione del codice di stato

app.Get("/not-found", func(c *fiber.Ctx) error {
  // Impostare il codice di stato della risposta
  return c.SendStatus(415)
  // => 415 "Tipo di media non supportato"

  c.SendString("Ciao, Mondo!")
  // Impostare il codice di stato della risposta
  return c.SendStatus(415)
  // => 415 "Ciao, Mondo!"
})

Restituzione dei file (Implementare il download del file)

app.Get("/", func(c *fiber.Ctx) error {
  return c.Download("./files/report-12345.pdf");
  // => Scarica report-12345.pdf

  return c.Download("./files/report-12345.pdf", "report.pdf");
  // => Scarica report.pdf
})

Impostazione degli header della richiesta

app.Get("/", func(c *fiber.Ctx) error {
  c.Set("Content-Type", "text/plain")
  // => "Content-type: text/plain"

  // ...
})

Impostazione dei cookie

Definizione del cookie

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"`
}

Restituzione del cookie

app.Get("/", func(c *fiber.Ctx) error {
  // Creare il cookie
  cookie := new(fiber.Cookie)
  cookie.Name = "john"
  cookie.Value = "doe"
  cookie.Expires = time.Now().Add(24 * time.Hour)

  // Impostare il cookie
  c.Cookie(cookie)
  // ...
})

Reindirizzamento (301 o 302)

Il valore predefinito è un reindirizzamento 302

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)
})