এই অধ্যায়টি প্রস্তুত করে যা জো ফাইবারের অনুরোধ এবং প্রতিক্রিয়া নিয়ে, সাধারণভাবে বর্তমান API উন্নতির জন্য JSON ডেটা প্রদান করে।

ফাইবার ফ্রেমওয়ার্ক প্রাথমিকভাবে Ctx সন্দর্ভ অবজেক্টের রিটার্ন ডেটা ব্যবহার করে।

JSON ডেটা প্রদান

type SomeStruct struct {
  Name string
  Age  uint8
}

app.Get("/json", func(c *fiber.Ctx) error {
  // রিসপন্সের জন্য ডেটা স্ট্রাকচার তৈরি
  data := SomeStruct{
    Name: "Grame",
    Age:  20,
  }

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

JSONP ফরম্যাট প্রদান

type SomeStruct struct {
  name string
  age  uint8
}

app.Get("/", func(c *fiber.Ctx) error {
  // ডেটা স্ট্রাকচার তৈরি
  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})
})

টেক্সট প্রদান

app.Get("/", func(c *fiber.Ctx) error {
        // "Hello, World ?!" এর মধ্যে স্ট্রিং হিসেবে রিটার্ন করুন
        return c.SendString("Hello, World ?!")
})

স্ট্যাটাস কোড প্রদান

app.Get("/not-found", func(c *fiber.Ctx) error {
  // রেসপন্স স্ট্যাটাস কোড সেট করুন
  return c.SendStatus(415)
  // => 415 "Unsupported Media Type"

  c.SendString("Hello, World!")
  // রেসপন্স স্ট্যাটাস কোড সেট করুন
  return c.SendStatus(415)
  // => 415 "Hello, World!"
})

ফাইল প্রদান (ফাইল ডাউনলোড বাস্তবায়ন)

app.Get("/", func(c *fiber.Ctx) error {
  return c.Download("./files/report-12345.pdf");
  // => রিপোর্ট-12345.pdf ডাউনলোড করুন

  return c.Download("./files/report-12345.pdf", "report.pdf");
  // => রিপোর্ট.pdf ডাউনলোড করুন
})

রিকোয়েস্ট হেডার সেট করুন

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

  // ...
})

কুকিজ সেট করুন

কুকি সংজ্ঞানা

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

কুকি প্রদান

app.Get("/", func(c *fiber.Ctx) error {
  // কুকি তৈরি
  cookie := new(fiber.Cookie)
  cookie.Name = "john"
  cookie.Value = "doe"
  cookie.Expires = time.Now().Add(24 * time.Hour)

  // কুকি সেট করুন
  c.Cookie(cookie)
  // ...
})

রিডাইরেক্ট (301 অথবা 302)

ডিফল্টভাবে 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)
})