mirror of
https://github.com/gofiber/fiber.git
synced 2025-05-31 11:52:41 +00:00
Update doc
This commit is contained in:
parent
e9a4f6e360
commit
6616af0125
@ -67,22 +67,22 @@ The following examples illustrate defining simple routes.
|
|||||||
```go
|
```go
|
||||||
// Respond with Hello, World! on the homepage:
|
// Respond with Hello, World! on the homepage:
|
||||||
app.Get("/", func(c *fiber.Ctx) {
|
app.Get("/", func(c *fiber.Ctx) {
|
||||||
c.Send("Hello, World!")
|
c.Send("Hello, World!")
|
||||||
})
|
})
|
||||||
|
|
||||||
//Respond to POST request on the root route (/), the application’s home page:
|
//Respond to POST request on the root route (/), the application’s home page:
|
||||||
app.Post("/", func(c *fiber.Ctx) {
|
app.Post("/", func(c *fiber.Ctx) {
|
||||||
c.Send("Got a POST request")
|
c.Send("Got a POST request")
|
||||||
})
|
})
|
||||||
|
|
||||||
// Respond to a PUT request to the /user route:
|
// Respond to a PUT request to the /user route:
|
||||||
app.Put("/user", func(c *fiber.Ctx) {
|
app.Put("/user", func(c *fiber.Ctx) {
|
||||||
c.Send("Got a PUT request at /user")
|
c.Send("Got a PUT request at /user")
|
||||||
})
|
})
|
||||||
|
|
||||||
// Respond to a DELETE request to the /user route:
|
// Respond to a DELETE request to the /user route:
|
||||||
app.Delete("/user", func(c *fiber.Ctx) {
|
app.Delete("/user", func(c *fiber.Ctx) {
|
||||||
c.Send("Got a DELETE request at /user")
|
c.Send("Got a DELETE request at /user")
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
164
docs/context.md
164
docs/context.md
@ -149,18 +149,18 @@ c.Cookies(func(key, value string)) string
|
|||||||
// Example
|
// Example
|
||||||
app.Get("/", func(c *fiber.Ctx) {
|
app.Get("/", func(c *fiber.Ctx) {
|
||||||
// Get raw cookie header
|
// Get raw cookie header
|
||||||
c.Cookies()
|
c.Cookies()
|
||||||
// => name=john;
|
// => name=john;
|
||||||
|
|
||||||
// Get cookie by key
|
// Get cookie by key
|
||||||
c.Cookies("name")
|
c.Cookies("name")
|
||||||
// => "john"
|
// => "john"
|
||||||
|
|
||||||
// Show all cookies
|
// Show all cookies
|
||||||
c.Cookies(func(key, val string) {
|
c.Cookies(func(key, val string) {
|
||||||
fmt.Println(key, val)
|
fmt.Println(key, val)
|
||||||
// => "name", "john"
|
// => "name", "john"
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -174,10 +174,10 @@ c.Download(path, filename string)
|
|||||||
// Example
|
// Example
|
||||||
app.Get("/", func(c *fiber.Ctx) {
|
app.Get("/", func(c *fiber.Ctx) {
|
||||||
c.Download("./files/report-12345.pdf")
|
c.Download("./files/report-12345.pdf")
|
||||||
// => Download report-12345.pdf
|
// => Download report-12345.pdf
|
||||||
|
|
||||||
c.Download("./files/report-12345.pdf", "report.pdf")
|
c.Download("./files/report-12345.pdf", "report.pdf")
|
||||||
// => Download report.pdf
|
// => Download report.pdf
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -193,11 +193,11 @@ c.Fasthttp...
|
|||||||
|
|
||||||
// Example
|
// Example
|
||||||
app.Get("/", func(c *fiber.Ctx) {
|
app.Get("/", func(c *fiber.Ctx) {
|
||||||
string(c.Fasthttp.Request.Header.Method())
|
c.Fasthttp.Request.Header.Method()
|
||||||
// => "GET"
|
// => []byte("GET")
|
||||||
|
|
||||||
c.Fasthttp.Response.Write([]byte("Hello, World!"))
|
c.Fasthttp.Response.Write([]byte("Hello, World!"))
|
||||||
// => "Hello, World!"
|
// => "Hello, World!"
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -213,7 +213,7 @@ c.FormFile(name string) (*multipart.FileHeader, error)
|
|||||||
// Example
|
// Example
|
||||||
app.Post("/", func(c *fiber.Ctx) {
|
app.Post("/", func(c *fiber.Ctx) {
|
||||||
// Get first file from form field "document"
|
// Get first file from form field "document"
|
||||||
file, err := c.FormFile("document")
|
file, err := c.FormFile("document")
|
||||||
|
|
||||||
// Check for errors
|
// Check for errors
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -232,7 +232,7 @@ c.FormValue(name string) string
|
|||||||
// Example
|
// Example
|
||||||
app.Post("/", func(c *fiber.Ctx) {
|
app.Post("/", func(c *fiber.Ctx) {
|
||||||
// Get first value from form field "name"
|
// Get first value from form field "name"
|
||||||
c.FormValue("name")
|
c.FormValue("name")
|
||||||
// => "john" or "" if not exist
|
// => "john" or "" if not exist
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -311,15 +311,15 @@ c.Is(typ string) bool
|
|||||||
|
|
||||||
// Example
|
// Example
|
||||||
app.Get("/", func(c *fiber.Ctx) {
|
app.Get("/", func(c *fiber.Ctx) {
|
||||||
// Content-Type: text/html; charset=utf-8
|
// Content-Type: text/html; charset=utf-8
|
||||||
c.Is("html")
|
c.Is("html")
|
||||||
// => true
|
// => true
|
||||||
|
|
||||||
c.Is(".html")
|
c.Is(".html")
|
||||||
// => true
|
// => true
|
||||||
|
|
||||||
c.Is("json")
|
c.Is("json")
|
||||||
// => false
|
// => false
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -331,19 +331,19 @@ c.Json(v interface{}) error
|
|||||||
|
|
||||||
// Example
|
// Example
|
||||||
type SomeStruct struct {
|
type SomeStruct struct {
|
||||||
Name string
|
Name string
|
||||||
Age uint8
|
Age uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
app := fiber.New()
|
app := fiber.New()
|
||||||
app.Get("/json", func(c *fiber.Ctx) {
|
app.Get("/json", func(c *fiber.Ctx) {
|
||||||
data := SomeStruct{
|
data := SomeStruct{
|
||||||
Name: "Grame",
|
Name: "Grame",
|
||||||
Age: 20,
|
Age: 20,
|
||||||
}
|
}
|
||||||
c.Json(data)
|
c.Json(data)
|
||||||
|
|
||||||
// Or with error checking
|
// Or with error checking
|
||||||
if err := c.Json(data); err != nil {
|
if err := c.Json(data); err != nil {
|
||||||
c.Status(500).Send("Bad Request")
|
c.Status(500).Send("Bad Request")
|
||||||
}
|
}
|
||||||
@ -360,24 +360,24 @@ Sends a JSON response with JSONP support. This method is identical to [Json()](#
|
|||||||
By default, the JSONP callback name is simply callback. Override this by passing a named string in the function.
|
By default, the JSONP callback name is simply callback. Override this by passing a named string in the function.
|
||||||
```go
|
```go
|
||||||
// Function signature
|
// Function signature
|
||||||
c.Jsonp(json interface{}) error
|
c.Jsonp(v interface{}) error
|
||||||
c.Jsonp(callback string, v interface{}) error
|
c.Jsonp(v interface{}, callback string) error
|
||||||
// Example
|
// Example
|
||||||
type JsonStruct struct {
|
type JsonStruct struct {
|
||||||
name string
|
name string
|
||||||
age uint8
|
age uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
app := fiber.New()
|
app := fiber.New()
|
||||||
app.Get("/", func(c *fiber.Ctx) {
|
app.Get("/", func(c *fiber.Ctx) {
|
||||||
data := JsonStruct{
|
data := JsonStruct{
|
||||||
name: "Grame",
|
name: "Grame",
|
||||||
age: 20,
|
age: 20,
|
||||||
}
|
}
|
||||||
c.Jsonp(data)
|
c.Jsonp(data)
|
||||||
// => callback({"name": "Grame", "age": 20})
|
// => callback({"name": "Grame", "age": 20})
|
||||||
|
|
||||||
c.Jsonp("customFunc", data)
|
c.Jsonp(data, "customFunc")
|
||||||
// => customFunc({"name": "Grame", "age": 20})
|
// => customFunc({"name": "Grame", "age": 20})
|
||||||
})
|
})
|
||||||
app.Listen(8080)
|
app.Listen(8080)
|
||||||
@ -397,8 +397,8 @@ c.Location(path string)
|
|||||||
|
|
||||||
// Example
|
// Example
|
||||||
app.Post("/", func(c *fiber.Ctx) {
|
app.Post("/", func(c *fiber.Ctx) {
|
||||||
c.Location("http://example.com")
|
c.Location("http://example.com")
|
||||||
c.Location("/foo/bar")
|
c.Location("/foo/bar")
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -410,8 +410,8 @@ c.Method() string
|
|||||||
|
|
||||||
// Example
|
// Example
|
||||||
app.Post("/", func(c *fiber.Ctx) {
|
app.Post("/", func(c *fiber.Ctx) {
|
||||||
c.Method()
|
c.Method()
|
||||||
// => "POST"
|
// => "POST"
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -425,28 +425,28 @@ c.MultipartForm() (*multipart.Form, error)
|
|||||||
|
|
||||||
// Example
|
// Example
|
||||||
app.Post("/", func(c *fiber.Ctx) {
|
app.Post("/", func(c *fiber.Ctx) {
|
||||||
// Parse the multipart form
|
// Parse the multipart form
|
||||||
if form, err := c.MultipartForm(); err == nil {
|
if form, err := c.MultipartForm(); err == nil {
|
||||||
// => *multipart.Form
|
// => *multipart.Form
|
||||||
|
|
||||||
if token := form.Value["token"]; len(token) > 0 {
|
if token := form.Value["token"]; len(token) > 0 {
|
||||||
// Get key value
|
// Get key value
|
||||||
fmt.Println(token[0])
|
fmt.Println(token[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all files from "documents" key
|
// Get all files from "documents" key
|
||||||
files := form.File["documents"]
|
files := form.File["documents"]
|
||||||
// => []*multipart.FileHeader
|
// => []*multipart.FileHeader
|
||||||
|
|
||||||
// Loop trough files
|
// Loop trough files
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
fmt.Println(file.Filename, file.Size, file.Header["Content-Type"][0])
|
fmt.Println(file.Filename, file.Size, file.Header["Content-Type"][0])
|
||||||
// => "tutorial.pdf" 360641 "application/pdf"
|
// => "tutorial.pdf" 360641 "application/pdf"
|
||||||
|
|
||||||
// Save the files to disk
|
// Save the files to disk
|
||||||
c.SaveFile(file, fmt.Sprintf("./%s", file.Filename))
|
c.SaveFile(file, fmt.Sprintf("./%s", file.Filename))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -458,16 +458,16 @@ c.Next()
|
|||||||
|
|
||||||
// Example
|
// Example
|
||||||
app.Get("/", func(c *fiber.Ctx) {
|
app.Get("/", func(c *fiber.Ctx) {
|
||||||
fmt.Printl("1st route!")
|
fmt.Printl("1st route!")
|
||||||
c.Next()
|
c.Next()
|
||||||
})
|
})
|
||||||
app.Get("*", func(c *fiber.Ctx) {
|
app.Get("*", func(c *fiber.Ctx) {
|
||||||
fmt.Printl("2nd route!")
|
fmt.Printl("2nd route!")
|
||||||
c.Next()
|
c.Next()
|
||||||
})
|
})
|
||||||
app.Get("/", func(c *fiber.Ctx) {
|
app.Get("/", func(c *fiber.Ctx) {
|
||||||
fmt.Printl("3rd route!")
|
fmt.Printl("3rd route!")
|
||||||
c.Send("Hello, World!")
|
c.Send("Hello, World!")
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -479,9 +479,9 @@ c.OriginalUrl() string
|
|||||||
|
|
||||||
// Example
|
// Example
|
||||||
app.Get("/", func(c *fiber.Ctx) {
|
app.Get("/", func(c *fiber.Ctx) {
|
||||||
// GET /search?q=something
|
// GET /search?q=something
|
||||||
c.OriginalUrl()
|
c.OriginalUrl()
|
||||||
// => '/search?q=something'
|
// => '/search?q=something'
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -493,9 +493,9 @@ c.Params(param string) string
|
|||||||
|
|
||||||
// Example
|
// Example
|
||||||
app.Get("/user/:name", func(c *fiber.Ctx) {
|
app.Get("/user/:name", func(c *fiber.Ctx) {
|
||||||
// GET /user/tj
|
// GET /user/tj
|
||||||
c.Params("name")
|
c.Params("name")
|
||||||
// => "tj"
|
// => "tj"
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -507,9 +507,9 @@ c.Path() string
|
|||||||
|
|
||||||
// Example
|
// Example
|
||||||
app.Get("/users", func(c *fiber.Ctx) {
|
app.Get("/users", func(c *fiber.Ctx) {
|
||||||
// example.com/users?sort=desc
|
// example.com/users?sort=desc
|
||||||
c.Path()
|
c.Path()
|
||||||
// => "/users"
|
// => "/users"
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -522,8 +522,8 @@ c.Protocol() string
|
|||||||
|
|
||||||
// Example
|
// Example
|
||||||
app.Get("/", func(c *fiber.Ctx) {
|
app.Get("/", func(c *fiber.Ctx) {
|
||||||
c.Protocol()
|
c.Protocol()
|
||||||
// => "http"
|
// => "http"
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
#### Query
|
#### Query
|
||||||
@ -552,14 +552,14 @@ Redirects to the URL derived from the specified path, with specified status, a p
|
|||||||
```go
|
```go
|
||||||
// Function signature
|
// Function signature
|
||||||
c.Redirect(path string)
|
c.Redirect(path string)
|
||||||
c.Redirect(status int, path string)
|
c.Redirect(path string, status int)
|
||||||
|
|
||||||
// Example
|
// Example
|
||||||
app.Get("/", func(c *fiber.Ctx) {
|
app.Get("/", func(c *fiber.Ctx) {
|
||||||
c.Redirect("/foo/bar")
|
c.Redirect("/foo/bar")
|
||||||
c.Redirect("http://example.com")
|
|
||||||
c.Redirect(301, "http://example.com")
|
|
||||||
c.Redirect("../login")
|
c.Redirect("../login")
|
||||||
|
c.Redirect("http://example.com")
|
||||||
|
c.Redirect("http://example.com", 301)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user