From 1c9c21e294e419a4cc2224d4a1de8425c290d5f1 Mon Sep 17 00:00:00 2001 From: loveuer Date: Thu, 1 Feb 2024 18:06:07 +0800 Subject: [PATCH] chore: remove print --- ctx.go | 5 +---- xtest/basic/basic.http | 10 +++++++++- xtest/basic/main.go | 25 ++++++++++++++++++++++--- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/ctx.go b/ctx.go index c26910a..5040071 100644 --- a/ctx.go +++ b/ctx.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "log" "mime/multipart" "net" "net/http" @@ -144,8 +143,6 @@ func (c *Ctx) BodyParser(out interface{}) error { ctype = strings.ToLower(c.Request.Header.Get("Content-Type")) ) - log.Printf("BodyParser: Content-Type=%s", ctype) - ctype = parseVendorSpecificContentType(ctype) ctypeEnd := strings.IndexByte(ctype, ';') @@ -156,9 +153,9 @@ func (c *Ctx) BodyParser(out interface{}) error { if strings.HasSuffix(ctype, "json") { bs, err := io.ReadAll(c.Request.Body) if err != nil { - log.Printf("BodyParser: read all err=%v", err) return err } + _ = c.Request.Body.Close() c.Request.Body = io.NopCloser(bytes.NewReader(bs)) diff --git a/xtest/basic/basic.http b/xtest/basic/basic.http index 881d4c1..f0497fd 100644 --- a/xtest/basic/basic.http +++ b/xtest/basic/basic.http @@ -3,4 +3,12 @@ GET http://127.0.0.1/hello/nf ### test resp error -GET http://127.0.0.1/error \ No newline at end of file +GET http://127.0.0.1/error + +### test basic post +POST http://127.0.0.1/data +Content-Type: application/json + +{ + "name": "nice" +} \ No newline at end of file diff --git a/xtest/basic/main.go b/xtest/basic/main.go index df46304..d438889 100644 --- a/xtest/basic/main.go +++ b/xtest/basic/main.go @@ -5,7 +5,6 @@ import ( "github.com/loveuer/nf" "github.com/loveuer/nf/nft/resp" "log" - "net" "time" ) @@ -25,7 +24,27 @@ func main() { app.Get("/error", func(c *nf.Ctx) error { return resp.RespError(c, resp.NewError(404, "not found", errors.New("NNNot Found"), nil)) }) + app.Post("/data", func(c *nf.Ctx) error { + type Req struct { + Name string `json:"name"` + } - ln, _ := net.Listen("tcp", ":80") - log.Fatal(app.RunListener(ln)) + var ( + err error + req = new(Req) + rm = make(map[string]any) + ) + + if err = c.BodyParser(req); err != nil { + return c.JSON(nf.Map{"status": 400, "msg": err.Error()}) + } + + if err = c.BodyParser(&rm); err != nil { + return c.JSON(nf.Map{"status": 400, "msg": err.Error()}) + } + + return c.JSON(nf.Map{"status": 200, "data": req, "map": rm}) + }) + + log.Fatal(app.Run(":80")) }