1 Star 0 Fork 0

micro-tools/wf

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
client_dump.go 1.85 KB
Copy Edit Raw Blame History
545403892 authored 2023-09-27 22:16 . 升级go-ole
package client
import (
"fmt"
"gitee.com/micro-tools/wf/internal/utils"
"io/ioutil"
"net/http"
"net/http/httputil"
"gitee.com/micro-tools/wf/util/gconv"
)
// dumpTextFormat is the format of the dumped raw string
const dumpTextFormat = `+---------------------------------------------+
| %s |
+---------------------------------------------+
%s
%s
`
// getResponseBody returns the text of the response body.
func getResponseBody(res *http.Response) string {
if res.Body == nil {
return ""
}
bodyContent, _ := ioutil.ReadAll(res.Body)
res.Body = utils.NewReadCloser(bodyContent, true)
return gconv.UnsafeBytesToStr(bodyContent)
}
// RawRequest returns the raw content of the request.
func (r *Response) RawRequest() string {
// Response can be nil.
if r == nil {
return ""
}
if r.request == nil {
return ""
}
// DumpRequestOut writes more request headers than DumpRequest, such as User-Agent.
bs, err := httputil.DumpRequestOut(r.request, false)
if err != nil {
return ""
}
return fmt.Sprintf(
dumpTextFormat,
"REQUEST ",
gconv.UnsafeBytesToStr(bs),
r.requestBody,
)
}
// RawResponse returns the raw content of the response.
func (r *Response) RawResponse() string {
// Response might be nil.
if r == nil || r.Response == nil {
return ""
}
bs, err := httputil.DumpResponse(r.Response, false)
if err != nil {
return ""
}
return fmt.Sprintf(
dumpTextFormat,
"RESPONSE",
gconv.UnsafeBytesToStr(bs),
getResponseBody(r.Response),
)
}
// Raw returns the raw text of the request and the response.
func (r *Response) Raw() string {
return fmt.Sprintf("%s\n%s", r.RawRequest(), r.RawResponse())
}
// RawDump outputs the raw text of the request and the response to stdout.
func (r *Response) RawDump() {
fmt.Println(r.Raw())
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/micro-tools/wf.git
git@gitee.com:micro-tools/wf.git
micro-tools
wf
wf
v1.0.2

Search