33package api
44
55import (
6+ "encoding/json"
7+ "fmt"
68 "net/http"
7-
8- "github.com/labstack/echo/v4"
9+ "strconv"
910)
1011
1112type User struct {
@@ -31,13 +32,34 @@ type FooBarResponse struct {
3132}
3233
3334// FooBarHandler handles incoming foobar requests
34- func FooBarHandler (ctx echo.Context ) error {
35- req := FooBarRequest {}
36- if err := ctx .Bind (& req ); err != nil {
37- return echo .ErrBadRequest
35+ func FooBarHandler (w http.ResponseWriter , req * http.Request ) {
36+ if err := req .ParseForm (); err != nil {
37+ http .Error (w , fmt .Sprintf ("%s: %v" , http .StatusText (http .StatusBadRequest ), err ), http .StatusBadRequest )
38+ return
39+ }
40+ raw := req .FormValue ("age" )
41+ age , err := strconv .Atoi (raw )
42+ if err != nil {
43+ http .Error (w , fmt .Sprintf ("%s: %v" , http .StatusText (http .StatusBadRequest ), err ), http .StatusBadRequest )
44+ return
45+ }
46+
47+ r := FooBarRequest {
48+ Foo : req .FormValue ("foo" ),
49+ User : User {
50+ Name : req .FormValue ("name" ),
51+ Age : age ,
52+ },
53+ }
54+
55+ resp := doSthWithRequest (r )
56+
57+ enc := json .NewEncoder (w )
58+ err = enc .Encode (resp )
59+ if err != nil {
60+ http .Error (w , fmt .Sprintf ("%s: %v" , http .StatusText (http .StatusInternalServerError ), err ), http .StatusInternalServerError )
61+ return
3862 }
39- resp := doSthWithRequest (req )
40- return ctx .JSON (http .StatusOK , resp )
4163}
4264
4365func doSthWithRequest (req FooBarRequest ) FooBarResponse {
0 commit comments