package main
import "github.com/gofiber/fiber"
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) {
c.JSON(fiber.Map{"code": 200, "message": "Hello, World"})
})
app.Listen(3000)
}
app.Post("/signup", func(c *fiber.Ctx) {
fmt.Println(c.Body())
})
// name=test&city=ewe
package main
import (
"fmt"
"log"
"github.com/gofiber/fiber"
)
type User struct {
Name string `json:"name"`
City string `json:"city"`
}
func main() {
app := fiber.New()
app.Post("/signup", func(c *fiber.Ctx) {
user := new(User)
if err := c.BodyParser(user); err != nil {
log.Fatal(err)
}
fmt.Println(user.Name)
})
app.Listen(3001)
}
struct 跟裡面的 field 記得要大寫
https://stackoverflow.com/a/24837507/4622645
app.Post("/signup", func(c *fiber.Ctx) {
...
fmt.Println(c.Get("authorization"))
app.Post("/message", func(c *fiber.Ctx) {
....do something
c.Next()
}, func(c *fiber.Ctx) {
....do something after
}
func Auth(c *fiber.Ctx) {
...
c.Next()
}
app.Post("/message", Auth, func(c *fiber.Ctx) {
...
}
app.Post("/message", func(c *fiber.Ctx) {
...
c.Context()
}
app.Get("/:name", func(c *fiber.Ctx) error {
msg := fmt.Sprintf("Hello, %s 👋!", c.Params("name"))
return c.SendString(msg)
})
app.Get("/", func(c *fiber.Ctx) error {
c.Query("order") // "desc"
c.Query("brand") // "nike"
c.Query("empty", "nike") // "nike"
})
func UploadAvatar(c *fiber.Ctx) error {
file, err := c.FormFile("document")
// Save file to root directory:
return c.SaveFile(file, fmt.Sprintf("./%s", file.Filename))
}