forked from tpkeeper/gin-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
71 lines (59 loc) · 1.59 KB
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package gindump
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
func DumpResponse(res *http.Response, showHeaders bool, showBody bool) string {
headerHiddenFields := make([]string, 0)
bodyHiddenFields := make([]string, 0)
var strB strings.Builder
//dump req header
s, err := FormatToBeautifulJson(res.Header, headerHiddenFields)
if showHeaders {
if err != nil {
strB.WriteString(fmt.Sprintf("\nparse resp header err \n" + err.Error()))
} else {
strB.WriteString("Response-Header:\n")
strB.WriteString(string(s))
}
}
if showBody {
bodyBytes, err := io.ReadAll(res.Body)
//reset the response body to the original unread state
res.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
if err != nil {
strB.WriteString(fmt.Sprintf("\nparse resp header err \n" + err.Error()))
}
//dump res body
if bodyAllowedForStatus(res.StatusCode) && len(bodyBytes) > 0 {
ctGet := res.Header.Get("Content-Type")
ct, _, err := mime.ParseMediaType(ctGet)
if err != nil {
strB.WriteString(fmt.Sprintf("\ncontent-type: %s parse err \n %s", ctGet, err.Error()))
goto End
}
switch ct {
case gin.MIMEJSON:
s, err := BeautifyJsonBytes(bodyBytes, bodyHiddenFields)
if err != nil {
strB.WriteString(fmt.Sprintf("\nparse bodyCache err \n" + err.Error()))
goto End
}
strB.WriteString("\nResponse-Body:\n")
strB.WriteString(string(s))
case gin.MIMEHTML:
default:
strB.WriteString("\nResponse-Body:\n")
strB.WriteString(string(bodyBytes))
}
}
}
End:
return strB.String()
}