From d84612a425476c8244772153a0c0ba75b620352e Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sat, 27 May 2023 18:11:40 +0200 Subject: [PATCH] add query string in the request log --- CHANGELOG.md | 4 ++++ capture.go | 15 +++++++++------ dashboard.html | 22 +++++++++++++++------- main.go | 20 +++++++++++++++++++- 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af9b5d8..4fe9789 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## [Unreleased] +## v1.1.0 +### Added +- add query string in the request log + ## v1.0.0 ### Added - remove "retry" button and add a button for each request in the list diff --git a/capture.go b/capture.go index f5f11e8..3591fac 100644 --- a/capture.go +++ b/capture.go @@ -27,12 +27,13 @@ type Capture struct { } type Req struct { - Proto string - Method string - Url string - Path string - Header http.Header - Body []byte + Proto string `json:"proto"` + Method string `json:"method"` + Url string `json:"url"` + Path string `json:"path"` + Query string `json:"query"` + Header http.Header `json:"header"` + Body []byte `json:"body"` } type Res struct { @@ -54,6 +55,7 @@ type CaptureInfo struct { type DashboardItem struct { ID int `json:"id"` Path string `json:"path"` + Query string `json:"query"` Method string `json:"method"` Status int `json:"status"` @@ -115,6 +117,7 @@ func (s *CaptureService) DashboardItems() []DashboardItem { metadatas[i] = DashboardItem{ ID: capture.ID, Path: capture.Req.Path, + Query: capture.Req.Query, Method: capture.Req.Method, Status: capture.Res.Code, Elapsed: capture.Elapsed, diff --git a/dashboard.html b/dashboard.html index e766bb7..9ed3ee1 100644 --- a/dashboard.html +++ b/dashboard.html @@ -185,6 +185,14 @@ color: var(--disabled); } + .query { + padding: 1rem; + font-family: inherit; + font-weight: 400; + line-height: 1.2em; + color: #fff; + } + pre { word-break: break-all; white-space: pre-wrap; @@ -330,11 +338,11 @@
- {{item.method}} - ‎{{item.path}}‎ - {{item.elapsed}}ms + {{ item.method }} + ‎{{ item.path }}‎ + {{ item.elapsed }}ms - {{item.status == 999 ? 'failed' : item.status}} + {{ item.status == 999 ? 'failed' : item.status }}
req
-
{{selectedItem.request}}
+
{{ selectedItem.request }}
@@ -360,14 +368,14 @@
res
-
{{selectedItem.response}}
+
{{ selectedItem.response }}

Waiting for requests on http://localhost:{{proxyPort}}/
- Proxying {{targetURL}} + Proxying {{ targetURL }}

diff --git a/main.go b/main.go index 75b4709..b7dee43 100644 --- a/main.go +++ b/main.go @@ -212,9 +212,11 @@ func NewRecorderHandler(srv *CaptureService, next http.HandlerFunc) http.Handler Method: r.Method, Url: r.URL.String(), Path: r.URL.Path, + Query: extractQueryString(r.RequestURI), Header: r.Header, Body: reqBody.Bytes(), } + res := Res{ Proto: rec.Result().Proto, Status: rec.Result().Status, @@ -246,8 +248,14 @@ func NewProxyHandler(URL string) http.HandlerFunc { func dump(c *Capture) CaptureInfo { req := c.Req res := c.Res + query := "" + + if len(req.Query) > 1 { + query = "?" + req.Query + } + return CaptureInfo{ - Request: dumpContent(req.Header, req.Body, "%s %s %s\n\n", req.Method, req.Path, req.Proto), + Request: dumpContent(req.Header, req.Body, "%s %s%s %s\n\n", req.Method, req.Path, query, req.Proto), Response: dumpContent(res.Header, res.Body, "%s %s\n\n", res.Proto, res.Status), Curl: dumpCurl(req), } @@ -296,3 +304,13 @@ func dumpCurl(req Req) string { } return b.String() } + +func extractQueryString(uri string) string { + parts := strings.SplitN(uri, "?", 2) + + if len(parts) != 2 { + return "" + } + + return parts[1] +}