mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-22 22:23:42 +00:00
.
This commit is contained in:
parent
856ef1c528
commit
f1fc41bb29
@ -1,4 +1,4 @@
|
||||
<img src="docs/logo.jpg" width="150" alt="accessibility text"><br><br>
|
||||
<img src="docs/static/logo.jpg" width="150" alt="Fiber"><br><br>
|
||||
[](https://github.com/fenny/fiber/releases/latest)
|
||||
[](http://godoc.org/github.com/fenny/fiber)
|
||||
[](https://goreportcard.com/report/github.com/fenny/fiber)
|
||||
|
@ -5,7 +5,7 @@ The ctx object represents the HTTP request and response and has methods for the
|
||||
!> Planned for V2
|
||||
|
||||
## Attachment
|
||||
Sets the HTTP response Content-Disposition header field to “attachment”. If a filename is given, then it sets the Content-Type based on the extension name via res.type(), and sets the Content-Disposition “filename=” parameter.
|
||||
Sets the HTTP response [Content-Disposition](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition) header field to “attachment”. If a filename is given, then it sets the Content-Type based on the extension name via (Type)[#type], and sets the Content-Disposition “filename=” parameter.
|
||||
```go
|
||||
// Function signature
|
||||
c.Attachment()
|
||||
@ -23,10 +23,10 @@ app.Get("/", func(c *fiber.Ctx) {
|
||||
```
|
||||
|
||||
## BasicAuth
|
||||
BasicAuth returns the username and password provided in the request's Authorization header, if the request uses HTTP Basic Authentication.
|
||||
BasicAuth returns the username and password provided in the request's Authorization header, if the request uses [HTTP Basic Authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication).
|
||||
```go
|
||||
// Function signature
|
||||
user, pass, ok := c.BasicAuth()
|
||||
c.BasicAuth() (user, pass string, ok bool)
|
||||
|
||||
// Example
|
||||
// curl --user john:doe http://localhost:8080
|
||||
@ -46,32 +46,36 @@ app.Get("/", func(c *fiber.Ctx) {
|
||||
|
||||
## Body
|
||||
Contains the raw post body submitted in the request.
|
||||
Calling a key in body returns a string value if exist or you loop trough the cookies using a function.
|
||||
Calling a key in body returns a string value if exist or you loop trough the body params using a key value function callback.
|
||||
|
||||
The following example shows how to use the body function.
|
||||
```go
|
||||
// Function signature
|
||||
c.Body()
|
||||
c.Body(key string)
|
||||
c.Body(func(key string, value string))
|
||||
c.Body() string
|
||||
c.Body(key string) string
|
||||
c.Body(func(key string, value string)) func(string, string)
|
||||
|
||||
// Example
|
||||
// curl -X POST http://localhost:8080 -d user=john
|
||||
app.Post("/", func(c *fiber.Ctx) {
|
||||
// Get the raw body post
|
||||
c.Body() // => user=john
|
||||
c.Body()
|
||||
// => user=john
|
||||
|
||||
// Get the body value using the key
|
||||
c.Body("user") // => "john"
|
||||
// Get the body value using specific key
|
||||
c.Body("user")
|
||||
// => "john"
|
||||
|
||||
// Loop trough all body params
|
||||
c.Body(func(key string, val string) {
|
||||
fmt.Printl(key, val) // => "user", "john"
|
||||
fmt.Printl(key, val)
|
||||
// => "user" "john"
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
## ClearCookies
|
||||
Clears all client cookies, or a specific cookie by name.
|
||||
Clears all client cookies or a specific cookie by name by setting the expire date in the past.
|
||||
```go
|
||||
// Function signature
|
||||
c.ClearCookies()
|
||||
@ -79,16 +83,16 @@ c.ClearCookies(key string)
|
||||
|
||||
// Example
|
||||
app.Get("/", func(c *fiber.Ctx) {
|
||||
// Delete all cookies from client side
|
||||
// Expires all cookies
|
||||
c.ClearCookies()
|
||||
|
||||
// Delete specific cookie
|
||||
// Expire specific cookie
|
||||
c.ClearCookies("user")
|
||||
})
|
||||
```
|
||||
|
||||
## Cookies
|
||||
Clears all cookies from client, or a specific cookie by name by adjusting the expiration.
|
||||
Get and set cookies
|
||||
```go
|
||||
// Function signature
|
||||
c.Cookies() string
|
||||
@ -99,17 +103,21 @@ c.Cookies(func(key string, value string))
|
||||
// Example
|
||||
app.Get("/", func(c *fiber.Ctx) {
|
||||
// Create cookie with key, value
|
||||
c.Cookies("name", "john") // => Cookie: name=john
|
||||
c.Cookies("name", "john")
|
||||
// => Cookie: name=john
|
||||
|
||||
// Get cookie by key
|
||||
c.Cookies("name") // => "john"
|
||||
c.Cookies("name")
|
||||
// => "john"
|
||||
|
||||
// Get raw cookie header
|
||||
c.Cookies() // => name=john;
|
||||
c.Cookies()
|
||||
// => name=john;
|
||||
|
||||
// Show all cookies
|
||||
c.Cookies(func(key string, val string) {
|
||||
fmt.Println(key, val) // => "name", "john"
|
||||
fmt.Println(key, val)
|
||||
// => "name", "john"
|
||||
})
|
||||
})
|
||||
```
|
||||
@ -156,7 +164,14 @@ c.FormFile(name string) (*multipart.FileHeader, error)
|
||||
|
||||
// Example
|
||||
app.Post("/", func(c *fiber.Ctx) {
|
||||
// Get first file from form field "document"
|
||||
file, err := c.FormFile("document")
|
||||
|
||||
// Check for errors
|
||||
if err == nil {
|
||||
// Save file to root directory
|
||||
c.SaveFile(file, fmt.Sprintf("./%s", file.Filename))
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
@ -168,7 +183,9 @@ c.FormValue(name string) string
|
||||
|
||||
// Example
|
||||
app.Post("/", func(c *fiber.Ctx) {
|
||||
// Get first value from form field "name"
|
||||
c.FormValue("name")
|
||||
// => "john" or "" if not exist
|
||||
})
|
||||
```
|
||||
|
||||
@ -182,13 +199,13 @@ c.Get(field string) string
|
||||
// Example
|
||||
app.Get("/", func(c *fiber.Ctx) {
|
||||
c.Get("Content-Type")
|
||||
// "text/plain"
|
||||
// => "text/plain"
|
||||
|
||||
c.Get("content-type")
|
||||
// "text/plain"
|
||||
// => "text/plain"
|
||||
|
||||
c.Get("something")
|
||||
// ""
|
||||
// => ""
|
||||
})
|
||||
```
|
||||
|
||||
@ -238,30 +255,34 @@ app.Get("/", func(c *fiber.Ctx) {
|
||||
// => false
|
||||
})
|
||||
```
|
||||
|
||||
## Json
|
||||
Converts any interface to json using [FFJson](https://github.com/pquerna/ffjson), this functions also sets the content header to application/json.
|
||||
```go
|
||||
// Function signature
|
||||
err := c.Json(v interface{})
|
||||
c.Json(v interface{}) error
|
||||
|
||||
// Example
|
||||
type SomeData struct {
|
||||
type SomeStruct struct {
|
||||
Name string
|
||||
Age uint8
|
||||
}
|
||||
|
||||
app := fiber.New()
|
||||
app.Get("/json", func(c *fiber.Ctx) {
|
||||
data := SomeData{
|
||||
data := SomeStruct{
|
||||
Name: "Grame",
|
||||
Age: 20,
|
||||
}
|
||||
c.Json(data)
|
||||
// or
|
||||
err := c.Json(data)
|
||||
if err != nil {
|
||||
// etc
|
||||
}
|
||||
|
||||
// Or with error checking
|
||||
if err := c.Json(data); err != nil {
|
||||
c.Status(500).Send("Bad Request")
|
||||
}
|
||||
// => "{"Name": "Grame", "Age": 20}"
|
||||
|
||||
|
||||
})
|
||||
app.Listen(8080)
|
||||
```
|
||||
@ -283,7 +304,7 @@ app.Post("/", func(c *fiber.Ctx) {
|
||||
```
|
||||
|
||||
## MultipartForm
|
||||
To access multipart form entries, you can parse the binary with .Form().
|
||||
To access multipart form entries, you can parse the binary with .MultipartForm().
|
||||
This returns a map[string][]string, so given a key the value will be a string slice.
|
||||
So accepting multiple files or values is easy, as shown below!
|
||||
```go
|
||||
@ -524,12 +545,19 @@ c.Write(body []byte)
|
||||
// Example
|
||||
app.Get("/", func(c *fiber.Ctx) {
|
||||
c.Write("Hello, ")
|
||||
// => "Hello, "
|
||||
|
||||
c.Write([]byte("World!"))
|
||||
// => "Hello, World!"
|
||||
|
||||
// Send sets the body, and does not append
|
||||
c.Send("My name is Jeff")
|
||||
// => "My name is Jeff"
|
||||
})
|
||||
```
|
||||
|
||||
## Xhr
|
||||
A Boolean property that is true if the request’s X-Requested-With header field is “XMLHttpRequest”, indicating that the request was issued by a client library such as jQuery.
|
||||
A Boolean property that is true if the request’s **X-Requested-With** header field is **XMLHttpRequest**, indicating that the request was issued by a client library such as [jQuery](https://api.jquery.com/jQuery.ajax/).
|
||||
```go
|
||||
// Function signature
|
||||
c.Xhr() bool
|
||||
|
113
docs/index.html
113
docs/index.html
@ -7,119 +7,10 @@
|
||||
<meta name="description" content="Description">
|
||||
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
|
||||
<link rel="stylesheet" href="//unpkg.com/docsify/lib/themes/vue.css">
|
||||
<link rel="stylesheet" href="static/style.css">
|
||||
<link href="https://fonts.googleapis.com/css?family=Fira+Mono&display=swap" rel="stylesheet">
|
||||
<link rel="shortcut icon" href="favicon.ico" />
|
||||
<link rel="shortcut icon" href="static/favicon.ico" />
|
||||
|
||||
<style>
|
||||
|
||||
|
||||
:focus {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
::-moz-focus-inner {
|
||||
border: 0;
|
||||
}
|
||||
|
||||
.sidebar-nav>ul>li>a {
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.app-sub-sidebar>li::before {
|
||||
content: "";
|
||||
padding-right: 0px;
|
||||
float: left;
|
||||
}
|
||||
|
||||
.app-sub-sidebar {
|
||||
margin: 0 0 0 7px !important;
|
||||
line-height: 1.2em !important;
|
||||
}
|
||||
|
||||
.app-sub-sidebar>li {
|
||||
margin: 0px 0 !important;
|
||||
}
|
||||
|
||||
.app-sub-sidebar>li>a {
|
||||
font-size: 12px !important;
|
||||
}
|
||||
|
||||
pre>code {
|
||||
font-family: 'Fira Mono' !important;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.token.function {
|
||||
color: hsl(207, 82%, 66%);
|
||||
}
|
||||
|
||||
.token.keyword {
|
||||
color: hsl(286, 60%, 67%);
|
||||
}
|
||||
|
||||
.token.string {
|
||||
color: hsl( 95, 38%, 62%);
|
||||
}
|
||||
|
||||
.token.boolean,
|
||||
.token.number {
|
||||
color: hsl( 29, 54%, 61%);
|
||||
}
|
||||
|
||||
code .token {
|
||||
color: hsl(286, 60%, 67%);
|
||||
}
|
||||
|
||||
.markdown-section pre>code,
|
||||
.markdown-section code,
|
||||
.markdown-section pre {
|
||||
background-color: #313440;
|
||||
}
|
||||
|
||||
.markdown-section pre>code,
|
||||
.token.punctuation {
|
||||
color: hsl(220, 14%, 71%);
|
||||
}
|
||||
|
||||
.token.cdata,
|
||||
.token.comment,
|
||||
.token.doctype,
|
||||
.token.prolog {
|
||||
color: hsl(220, 10%, 40%);
|
||||
}
|
||||
|
||||
.token.property {
|
||||
color: hsl(187, 47%, 55%);
|
||||
}
|
||||
|
||||
.sidebar-toggle {
|
||||
background: #42b983;
|
||||
}
|
||||
|
||||
.sidebar-toggle span {
|
||||
background-color: white;
|
||||
}
|
||||
|
||||
.sidebar-toggle {
|
||||
width: 36px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
@media screen and (max-width:768px) {
|
||||
.sidebar-toggle {
|
||||
padding: 10px;
|
||||
}
|
||||
body.close .sidebar-toggle {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
.sidebar-toggle .sidebar-toggle-button:hover {
|
||||
opacity: .8;
|
||||
}
|
||||
|
||||
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
|
0
docs/favicon.ico → docs/static/favicon.ico
vendored
0
docs/favicon.ico → docs/static/favicon.ico
vendored
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
0
docs/logo.jpg → docs/static/logo.jpg
vendored
0
docs/logo.jpg → docs/static/logo.jpg
vendored
Before Width: | Height: | Size: 50 KiB After Width: | Height: | Size: 50 KiB |
111
docs/static/style.css
vendored
Normal file
111
docs/static/style.css
vendored
Normal file
@ -0,0 +1,111 @@
|
||||
:focus {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
::-moz-focus-inner {
|
||||
border: 0;
|
||||
}
|
||||
|
||||
.sidebar-nav>ul>li>a {
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.app-sub-sidebar>li::before {
|
||||
content: "";
|
||||
padding-right: 0px;
|
||||
float: left;
|
||||
}
|
||||
|
||||
.app-sub-sidebar {
|
||||
margin: 0 0 0 7px !important;
|
||||
line-height: 1.2em !important;
|
||||
}
|
||||
|
||||
.app-sub-sidebar>li {
|
||||
margin: 0px 0 !important;
|
||||
}
|
||||
|
||||
.app-sub-sidebar>li>a {
|
||||
font-size: 12px !important;
|
||||
}
|
||||
|
||||
pre>code {
|
||||
font-family: 'Fira Mono' !important;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.token.function {
|
||||
color: hsl(207, 82%, 66%);
|
||||
}
|
||||
|
||||
.token.keyword {
|
||||
color: hsl(286, 60%, 67%);
|
||||
}
|
||||
|
||||
.token.string {
|
||||
color: hsl( 95, 38%, 62%);
|
||||
}
|
||||
|
||||
.token.boolean,
|
||||
.token.number {
|
||||
color: hsl( 29, 54%, 61%);
|
||||
}
|
||||
|
||||
code .token {
|
||||
color: hsl(286, 60%, 67%);
|
||||
}
|
||||
|
||||
.markdown-section pre>code,
|
||||
.markdown-section code,
|
||||
.markdown-section pre {
|
||||
background-color: #313440;
|
||||
}
|
||||
|
||||
.markdown-section pre>code,
|
||||
.token.punctuation {
|
||||
color: hsl(220, 14%, 71%);
|
||||
}
|
||||
|
||||
.token.cdata,
|
||||
.token.comment,
|
||||
.token.doctype,
|
||||
.token.prolog {
|
||||
color: hsl(220, 10%, 40%);
|
||||
}
|
||||
|
||||
.token.property {
|
||||
color: hsl(187, 47%, 55%);
|
||||
}
|
||||
|
||||
.sidebar-toggle {
|
||||
background: #42b983;
|
||||
}
|
||||
|
||||
.sidebar-toggle span {
|
||||
background-color: white;
|
||||
}
|
||||
|
||||
.sidebar-toggle {
|
||||
width: 36px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
@media screen and (max-width:768px) {
|
||||
.sidebar-toggle {
|
||||
padding: 10px;
|
||||
}
|
||||
body.close .sidebar-toggle {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
.sidebar-toggle .sidebar-toggle-button:hover {
|
||||
opacity: .8;
|
||||
}
|
||||
.sidebar {
|
||||
width: 250px;
|
||||
}
|
||||
.sidebar > h1 {
|
||||
text-align: left;
|
||||
margin: 0 0 0 15px;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user