mirror of
https://gitee.com/johng/gf.git
synced 2024-12-02 20:28:17 +08:00
improve dump feature for ghttp.Request
This commit is contained in:
parent
9b330adc1d
commit
0290de2360
@ -18,22 +18,12 @@ import (
|
||||
|
||||
// dumpTextFormat is the format of the dumped raw string
|
||||
const dumpTextFormat = `+---------------------------------------------+
|
||||
| %s |
|
||||
| %s |
|
||||
+---------------------------------------------+
|
||||
%s
|
||||
%s
|
||||
`
|
||||
|
||||
// getRequestBody returns the raw text of the request body.
|
||||
func getRequestBody(req *http.Request) string {
|
||||
if req.Body == nil {
|
||||
return ""
|
||||
}
|
||||
bodyContent, _ := ioutil.ReadAll(req.Body)
|
||||
req.Body = utils.NewReadCloser(bodyContent, true)
|
||||
return gconv.UnsafeBytesToStr(bodyContent)
|
||||
}
|
||||
|
||||
// getResponseBody returns the text of the response body.
|
||||
func getResponseBody(res *http.Response) string {
|
||||
if res.Body == nil {
|
||||
@ -60,9 +50,9 @@ func (r *ClientResponse) RawRequest() string {
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
dumpTextFormat,
|
||||
"REQUEST ",
|
||||
"REQUEST",
|
||||
gconv.UnsafeBytesToStr(bs),
|
||||
getRequestBody(r.request),
|
||||
r.requestBody,
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -223,10 +223,8 @@ func (c *Client) DoRequest(method, url string, data ...interface{}) (resp *Clien
|
||||
// The request body can be reused for dumping
|
||||
// raw HTTP request-response procedure.
|
||||
reqBodyContent, _ := ioutil.ReadAll(req.Body)
|
||||
resp.requestBody = reqBodyContent
|
||||
req.Body = utils.NewReadCloser(reqBodyContent, false)
|
||||
defer func() {
|
||||
resp.request.Body = utils.NewReadCloser(reqBodyContent, true)
|
||||
}()
|
||||
for {
|
||||
if resp.Response, err = c.Do(req); err != nil {
|
||||
if c.retryCount > 0 {
|
||||
|
@ -16,8 +16,9 @@ import (
|
||||
// ClientResponse is the struct for client request response.
|
||||
type ClientResponse struct {
|
||||
*http.Response
|
||||
request *http.Request
|
||||
cookies map[string]string
|
||||
request *http.Request
|
||||
requestBody []byte
|
||||
cookies map[string]string
|
||||
}
|
||||
|
||||
// initCookie initializes the cookie map attribute of ClientResponse.
|
||||
|
Loading…
Reference in New Issue
Block a user