2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-04-08 19:24:03 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
2021-12-03 23:32:00 +08:00
|
|
|
package gclient
|
2020-04-08 19:24:03 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
2021-11-13 23:23:55 +08:00
|
|
|
|
2021-12-21 22:59:14 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/intlog"
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/utils"
|
2020-04-08 19:24:03 +08:00
|
|
|
)
|
|
|
|
|
2020-04-11 12:16:53 +08:00
|
|
|
// dumpTextFormat is the format of the dumped raw string
|
2020-04-08 19:24:03 +08:00
|
|
|
const dumpTextFormat = `+---------------------------------------------+
|
2020-11-12 18:57:18 +08:00
|
|
|
| %s |
|
2020-04-08 19:24:03 +08:00
|
|
|
+---------------------------------------------+
|
|
|
|
%s
|
|
|
|
%s
|
|
|
|
`
|
|
|
|
|
2020-04-11 12:16:53 +08:00
|
|
|
// getResponseBody returns the text of the response body.
|
2020-06-06 13:34:58 +08:00
|
|
|
func getResponseBody(res *http.Response) string {
|
|
|
|
if res.Body == nil {
|
2020-04-08 19:24:03 +08:00
|
|
|
return ""
|
|
|
|
}
|
2020-06-06 13:34:58 +08:00
|
|
|
bodyContent, _ := ioutil.ReadAll(res.Body)
|
|
|
|
res.Body = utils.NewReadCloser(bodyContent, true)
|
2021-09-14 19:30:20 +08:00
|
|
|
return string(bodyContent)
|
2020-04-08 19:24:03 +08:00
|
|
|
}
|
|
|
|
|
2020-04-24 00:00:52 +08:00
|
|
|
// RawRequest returns the raw content of the request.
|
2021-01-25 14:54:38 +08:00
|
|
|
func (r *Response) RawRequest() string {
|
|
|
|
// Response can be nil.
|
2022-06-13 19:31:42 +08:00
|
|
|
if r == nil || r.request == nil {
|
2020-04-08 19:24:03 +08:00
|
|
|
return ""
|
|
|
|
}
|
2020-04-11 12:16:53 +08:00
|
|
|
// DumpRequestOut writes more request headers than DumpRequest, such as User-Agent.
|
2020-04-24 00:00:52 +08:00
|
|
|
bs, err := httputil.DumpRequestOut(r.request, false)
|
2020-04-08 19:24:03 +08:00
|
|
|
if err != nil {
|
2022-01-28 14:51:49 +08:00
|
|
|
intlog.Errorf(r.request.Context(), `%+v`, err)
|
2020-04-08 19:24:03 +08:00
|
|
|
return ""
|
|
|
|
}
|
2020-04-24 00:00:52 +08:00
|
|
|
return fmt.Sprintf(
|
|
|
|
dumpTextFormat,
|
2020-11-12 18:57:18 +08:00
|
|
|
"REQUEST ",
|
2021-09-14 19:30:20 +08:00
|
|
|
string(bs),
|
2020-06-09 20:46:04 +08:00
|
|
|
r.requestBody,
|
2020-04-24 00:00:52 +08:00
|
|
|
)
|
2020-04-08 19:24:03 +08:00
|
|
|
}
|
|
|
|
|
2020-04-24 00:00:52 +08:00
|
|
|
// RawResponse returns the raw content of the response.
|
2021-01-25 14:54:38 +08:00
|
|
|
func (r *Response) RawResponse() string {
|
2021-02-01 21:51:42 +08:00
|
|
|
// Response might be nil.
|
2020-04-08 19:24:03 +08:00
|
|
|
if r == nil || r.Response == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
bs, err := httputil.DumpResponse(r.Response, false)
|
|
|
|
if err != nil {
|
2022-01-28 14:51:49 +08:00
|
|
|
intlog.Errorf(r.request.Context(), `%+v`, err)
|
2020-04-08 19:24:03 +08:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-04-24 00:00:52 +08:00
|
|
|
return fmt.Sprintf(
|
|
|
|
dumpTextFormat,
|
|
|
|
"RESPONSE",
|
2021-09-14 19:30:20 +08:00
|
|
|
string(bs),
|
2020-04-24 00:00:52 +08:00
|
|
|
getResponseBody(r.Response),
|
|
|
|
)
|
2020-04-08 19:24:03 +08:00
|
|
|
}
|
|
|
|
|
2020-04-11 12:16:53 +08:00
|
|
|
// Raw returns the raw text of the request and the response.
|
2021-01-25 14:54:38 +08:00
|
|
|
func (r *Response) Raw() string {
|
2020-04-08 19:24:03 +08:00
|
|
|
return fmt.Sprintf("%s\n%s", r.RawRequest(), r.RawResponse())
|
|
|
|
}
|
2020-06-06 13:34:58 +08:00
|
|
|
|
2020-06-08 19:26:14 +08:00
|
|
|
// RawDump outputs the raw text of the request and the response to stdout.
|
2021-01-25 14:54:38 +08:00
|
|
|
func (r *Response) RawDump() {
|
2020-06-06 13:34:58 +08:00
|
|
|
fmt.Println(r.Raw())
|
|
|
|
}
|