From 856ef1c528a0b681b366291ffa5cce0ce8a70f48 Mon Sep 17 00:00:00 2001 From: Fenny Date: Wed, 8 Jan 2020 18:07:21 -0500 Subject: [PATCH] . --- context.go | 26 +++-------- docs/context.md | 119 ++++++++++++++++++++++++++++------------------- docs/examples.md | 2 +- docs/index.html | 17 +++++++ 4 files changed, 97 insertions(+), 67 deletions(-) diff --git a/context.go b/context.go index f9437c2a..25176234 100644 --- a/context.go +++ b/context.go @@ -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 : diff --git a/docs/context.md b/docs/context.md index 90e5751e..9be1eaa3 100644 --- a/docs/context.md +++ b/docs/context.md @@ -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 request’s “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 request’s 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) { diff --git a/docs/examples.md b/docs/examples.md index bf7f8f86..f15f8543 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -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 diff --git a/docs/index.html b/docs/index.html index 5e0a730e..d5147e22 100644 --- a/docs/index.html +++ b/docs/index.html @@ -135,6 +135,23 @@ }