Add BodyParser

pull/73/head
Fenny 2020-02-07 17:47:09 +01:00
parent 545815ab95
commit 11f7d46306
3 changed files with 14 additions and 2 deletions

View File

@ -2,7 +2,7 @@ language: go
os:
- linux
- windows
# - windows
- osx
go:
- 1.13.x

View File

@ -9,11 +9,13 @@ package fiber
import (
"encoding/base64"
"encoding/xml"
"fmt"
"mime"
"mime/multipart"
"strings"
jsoniter "github.com/json-iterator/go"
"github.com/valyala/fasthttp"
)
@ -194,6 +196,17 @@ func (ctx *Ctx) Body(args ...interface{}) string {
return ""
}
// BodyParser : https://gofiber.github.io/fiber/#/context?id=bodyparser
func (ctx *Ctx) BodyParser(v interface{}) error {
cType := getString(ctx.Fasthttp.Request.Header.ContentType())
if cType == contentTypeJSON {
return jsoniter.Unmarshal(ctx.Fasthttp.Request.Body(), v)
} else if cType == contentTypeXML {
return xml.Unmarshal(ctx.Fasthttp.Request.Body(), v)
}
return fmt.Errorf("Cannot Parse Content-Type: %v", cType)
}
// Cookies : https://gofiber.github.io/fiber/#/context?id=cookies
func (ctx *Ctx) Cookies(args ...interface{}) string {
if len(args) == 0 {

View File

@ -64,7 +64,6 @@ func getRegex(path string) (*regexp.Regexp, error) {
}
func getFiles(root string) (files []string, isDir bool, err error) {
root = filepath.Clean(root)
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, path)