From 1eac43241745b46aa16fdae17945409f8cf85203 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sat, 25 Sep 2021 02:56:13 +0700 Subject: example: return all response like HTTP Request in JSON format The endpoint for GET and POST examples now return the object like HTTP Request which contains the HTTP Method, URL, Headers, Form, MultipartForm, and the Body. This will allow us to inspect the request with logging in to console. --- example/example.go | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/example/example.go b/example/example.go index 3bf7395..3d582eb 100644 --- a/example/example.go +++ b/example/example.go @@ -8,7 +8,9 @@ import ( "context" "encoding/json" "fmt" + "mime/multipart" "net/http" + "net/url" "sync" "time" @@ -30,6 +32,15 @@ const ( websocketAddress = "127.0.0.1:28240" ) +type requestResponse struct { + Method string + Url string + Headers http.Header + Form url.Values + MultipartForm *multipart.Form + Body string +} + type Example struct { trunks *trunks.Trunks wsServer *websocket.Server @@ -128,7 +139,7 @@ func (ex *Example) registerEndpoints() (err error) { Path: pathExample, RequestType: libhttp.RequestTypeForm, ResponseType: libhttp.ResponseTypeJSON, - Call: ex.pathExamplePostForm, + Call: ex.pathExamplePost, }) return err @@ -310,10 +321,19 @@ func (ex *Example) registerTargets() (err error) { } func (ex *Example) pathExampleGet(epr *libhttp.EndpointRequest) ([]byte, error) { + data := &requestResponse{ + Method: epr.HttpRequest.Method, + Url: epr.HttpRequest.URL.String(), + Headers: epr.HttpRequest.Header, + Form: epr.HttpRequest.Form, + MultipartForm: epr.HttpRequest.MultipartForm, + Body: string(epr.RequestBody), + } + res := libhttp.EndpointResponse{} res.Code = http.StatusOK res.Message = pathExample - res.Data = epr.HttpRequest.Form + res.Data = data return json.Marshal(&res) } @@ -322,11 +342,22 @@ func (ex *Example) pathExampleErrorGet(epr *libhttp.EndpointRequest) ([]byte, er return nil, liberrors.Internal(fmt.Errorf("server error")) } -func (ex *Example) pathExamplePostForm(epr *libhttp.EndpointRequest) ([]byte, error) { +func (ex *Example) pathExamplePost(epr *libhttp.EndpointRequest) ([]byte, error) { + epr.HttpRequest.ParseMultipartForm(0) + + data := &requestResponse{ + Method: epr.HttpRequest.Method, + Url: epr.HttpRequest.URL.String(), + Headers: epr.HttpRequest.Header, + Form: epr.HttpRequest.Form, + MultipartForm: epr.HttpRequest.MultipartForm, + Body: string(epr.RequestBody), + } + res := libhttp.EndpointResponse{} res.Code = http.StatusOK res.Message = pathExample - res.Data = epr.HttpRequest.Form + res.Data = data return json.Marshal(&res) } -- cgit v1.3