pull/6/head
Fenny 2020-01-08 18:07:21 -05:00
parent 0267a020ca
commit 856ef1c528
4 changed files with 97 additions and 67 deletions

View File

@ -70,31 +70,19 @@ func (ctx *Ctx) BasicAuth() (user, pass string, ok bool) {
return cs[:s], cs[s+1:], true
}
// Form :
func (ctx *Ctx) Form() *multipart.Form {
form, err := ctx.Fasthttp.MultipartForm()
if err != nil {
return nil
}
return form
// MultipartForm :
func (ctx *Ctx) MultipartForm() (*multipart.Form, error) {
return ctx.Fasthttp.MultipartForm()
}
// FormFile :
func (ctx *Ctx) FormValue(key string) *multipart.Form {
form, err := ctx.Fasthttp.MultipartForm()
if err != nil {
return nil
}
return form
func (ctx *Ctx) FormValue(key string) string {
return b2s(ctx.Fasthttp.FormValue(key))
}
// FormFile :
func (ctx *Ctx) FormFile(key string) *multipart.Form {
form, err := ctx.Fasthttp.MultipartForm()
if err != nil {
return nil
}
return form
func (ctx *Ctx) FormFile(key string) (*multipart.FileHeader, error) {
return ctx.Fasthttp.FormFile(key)
}
// SaveFile :

View File

@ -34,12 +34,12 @@ app.Get("/", func(c *fiber.Ctx) {
user, pass, ok := c.BasicAuth()
if ok && user == "john" && pass == "doe" {
c.Send("Welcome " + user)
return
if !ok || user != "john" || pass != "doe" {
c.Status(403).Send("Forbidden")
return
}
c.Status(403).Send("Forbidden")
c.Send("Welcome " + user)
})
```
@ -91,9 +91,9 @@ app.Get("/", func(c *fiber.Ctx) {
Clears all cookies from client, or a specific cookie by name by adjusting the expiration.
```go
// Function signature
c.Cookies()
c.Cookies(key string)
c.Cookies(key string, value string)
c.Cookies() string
c.Cookies(key string) string
c.Cookies(key string, value string) string
c.Cookies(func(key string, value string))
// Example
@ -148,46 +148,36 @@ app.Get("/", func(c *fiber.Ctx) {
})
```
## Form
To access multipart form entries, you can parse the binary with .Form().
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!
## FormFile
MultipartForm files can be retrieved by name, the first file from the given key is returned.
```go
// Function signature
c.Form()
c.FormFile(name string) (*multipart.FileHeader, error)
// Example
app.Post("/", func(c *fiber.Ctx) {
// Parse the multipart form
if form := c.Form(); form != nil {
// => *multipart.Form
if token := form.Value["token"]; len(token) > 0 {
// Get key value
fmt.Println(token[0])
}
// Get all files from "documents" key
files := form.File["documents"]
// => []*multipart.FileHeader
// Loop trough files
for _, file := range files {
fmt.Println(file.Filename, file.Size, file.Header["Content-Type"][0])
// => "tutorial.pdf" 360641 "application/pdf"
// Save the files to disk
c.SaveFile(file, fmt.Sprintf("./%s", file.Filename))
}
}
file, err := c.FormFile("document")
})
```
## FormValue
MultipartForm values can be retrieved by name, the first value from the given key is returned.
```go
// Function signature
c.FormValue(name string) string
// Example
app.Post("/", func(c *fiber.Ctx) {
c.FormValue("name")
})
```
## Get
Returns the HTTP response header specified by field. The match is case-insensitive.
```go
// Function signature
c.Get(field string)
c.Get(field string) string
// Example
app.Get("/", func(c *fiber.Ctx) {
@ -206,7 +196,7 @@ app.Get("/", func(c *fiber.Ctx) {
Contains the hostname derived from the Host HTTP header.
```go
// Function signature
c.Hostname()
c.Hostname() string
// Example
app.Get("/", func(c *fiber.Ctx) {
@ -220,7 +210,7 @@ app.Get("/", func(c *fiber.Ctx) {
Contains the remote IP address of the request.
```go
// Function signature
c.IP()
c.IP() string
// Example
app.Get("/", func(c *fiber.Ctx) {
@ -233,7 +223,7 @@ app.Get("/", func(c *fiber.Ctx) {
Returns the matching content type if the incoming requests “Content-Type” HTTP header field matches the MIME type specified by the type parameter. If the request has no body, returns false.
```go
// Function signature
c.IP(typ string)
c.Is(typ string) bool
// Example
app.Get("/", func(c *fiber.Ctx) {
@ -283,7 +273,7 @@ app.Listen(8080)
Contains a string corresponding to the HTTP method of the request: GET, POST, PUT, and so on.
```go
// Function signature
c.Method()
c.Method() string
// Example
app.Post("/", func(c *fiber.Ctx) {
@ -292,6 +282,41 @@ app.Post("/", func(c *fiber.Ctx) {
})
```
## MultipartForm
To access multipart form entries, you can parse the binary with .Form().
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
// Function signature
c.MultipartForm() (*multipart.Form, error)
// Example
app.Post("/", func(c *fiber.Ctx) {
// Parse the multipart form
if form, err := c.MultipartForm(); err == nil {
// => *multipart.Form
if token := form.Value["token"]; len(token) > 0 {
// Get key value
fmt.Println(token[0])
}
// Get all files from "documents" key
files := form.File["documents"]
// => []*multipart.FileHeader
// Loop trough files
for _, file := range files {
fmt.Println(file.Filename, file.Size, file.Header["Content-Type"][0])
// => "tutorial.pdf" 360641 "application/pdf"
// Save the files to disk
c.SaveFile(file, fmt.Sprintf("./%s", file.Filename))
}
}
})
```
## Next
When Next() is called, it executes the next function in the stack that matches the current route.
```go
@ -317,7 +342,7 @@ app.Get("/", func(c *fiber.Ctx) {
Contains the original request URL.
```go
// Function signature
c.OriginalURL()
c.OriginalURL() string
// Example
app.Get("/", func(c *fiber.Ctx) {
@ -331,7 +356,7 @@ app.Get("/", func(c *fiber.Ctx) {
This method can be used to get the route parameters. For example, if you have the route /user/:name, then the “name” property is available as c.Params("name"). This method defaults "".
```go
// Function signature
c.Params(param string)
c.Params(param string) string
// Example
app.Get("/user/:name", func(c *fiber.Ctx) {
@ -345,7 +370,7 @@ app.Get("/user/:name", func(c *fiber.Ctx) {
Contains the path part of the request URL.
```go
// Function signature
c.Path()
c.Path() string
// Example
app.Get("/users", func(c *fiber.Ctx) {
@ -360,7 +385,7 @@ Contains the request protocol string: either http or (for TLS requests) https.
```go
// Function signature
c.Protocol()
c.Protocol() string
// Example
app.Get("/", func(c *fiber.Ctx) {
@ -372,7 +397,7 @@ app.Get("/", func(c *fiber.Ctx) {
This property is an object containing a property for each query string parameter in the route. If there is no query string, it returns an empty string
```go
// Function signature
c.Query(parameter string)
c.Query(parameter string) string
// Example
app.Get("/", func(c *fiber.Ctx) {
@ -403,7 +428,7 @@ app.Get("/", func(c *fiber.Ctx) {
## SaveFile
This function is used to save any multipart file to disk.
You can see a working example here: [Multiple file upload](#form)
You can see a working example here: [Multiple file upload](#multipartform)
```go
// Function signature
@ -469,7 +494,7 @@ app.Get("/", func(c *fiber.Ctx) {
Sets the Content-Type HTTP header to the MIME type as determined by mime.lookup() for the specified type. If type contains the “/” character, then it sets the Content-Type to type.
```go
// Function signature
c.Type(typ string)
c.Type(typ string) string
// Example
app.Get("/", func(c *fiber.Ctx) {
@ -507,7 +532,7 @@ app.Get("/", func(c *fiber.Ctx) {
A Boolean property that is true if the requests X-Requested-With header field is “XMLHttpRequest”, indicating that the request was issued by a client library such as jQuery.
```go
// Function signature
c.Xhr()
c.Xhr() bool
// Example
app.Get("/", func(c *fiber.Ctx) {

View File

@ -11,7 +11,7 @@ func main() {
app := fiber.New()
app.Post("/", func(c *fiber.Ctx) {
// Parse the multipart form
if form := c.Form(); form != nil {
if form := c.MultipartForm(); form != nil {
// => *multipart.Form
// Get all files from "documents" key

View File

@ -135,6 +135,23 @@
}
</script>
<script type="text/javascript">
Prism.languages.curl = Prism.languages.extend('clike', {
'curl': /\bcurl\b/,
'url': /https?:[a-zA-Z0-9:.?=\/\-_{}]*/,
'parameter': {
pattern: /[A-Za-z0-9\[\]-_]+ *(?=[=])/,
},
'value': [{
pattern: /([=])([A-Za-z0-9-_.]*)/,
lookbehind: true,
}, {
pattern: /(["'])(\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
}, {
pattern: /(\-u )([A-Za-z0-9-_.{}]*)/,
lookbehind: true,
}],
'option': / *-[a-zA-Z]*\b/,
});
Prism.languages.go = Prism.languages.extend('clike', {
'keyword': /\b(?:break|case|chan|const|continue|default|defer|else|fallthrough|for|func|go(?:to)?|if|import|interface|map|package|range|return|select|struct|switch|type|var)\b/,
'builtin': /\b(?:bool|byte|complex(?:64|128)|error|float(?:32|64)|rune|string|u?int(?:8|16|32|64)?|uintptr|append|cap|close|complex|copy|delete|imag|len|make|new|panic|print(?:ln)?|real|recover)\b/,