2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2017-12-29 16:03:30 +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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-31 18:19:58 +08:00
|
|
|
|
2021-12-03 23:32:00 +08:00
|
|
|
package gclient
|
2017-12-07 14:57:16 +08:00
|
|
|
|
|
|
|
|
import (
|
2022-06-17 11:31:32 +08:00
|
|
|
"bytes"
|
2023-08-01 21:15:28 +08:00
|
|
|
"io"
|
2019-06-19 09:06:52 +08:00
|
|
|
"net/http"
|
2021-12-21 22:59:14 +08:00
|
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/internal/intlog"
|
2017-12-07 14:57:16 +08:00
|
|
|
)
|
|
|
|
|
|
2021-01-25 14:54:38 +08:00
|
|
|
// Response is the struct for client request response.
|
|
|
|
|
type Response struct {
|
2022-01-10 16:42:30 +08:00
|
|
|
*http.Response // Response is the underlying http.Response object of certain request.
|
|
|
|
|
request *http.Request // Request is the underlying http.Request object of certain request.
|
|
|
|
|
requestBody []byte // The body bytes of certain request, only available in Dump feature.
|
|
|
|
|
cookies map[string]string // Response cookies, which are only parsed once.
|
2019-02-26 17:17:11 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-25 14:54:38 +08:00
|
|
|
// initCookie initializes the cookie map attribute of Response.
|
|
|
|
|
func (r *Response) initCookie() {
|
2020-03-19 22:56:12 +08:00
|
|
|
if r.cookies == nil {
|
|
|
|
|
r.cookies = make(map[string]string)
|
2021-02-24 01:07:09 +08:00
|
|
|
// Response might be nil.
|
2022-11-01 20:12:21 +08:00
|
|
|
if r.Response != nil {
|
2021-02-24 01:07:09 +08:00
|
|
|
for _, v := range r.Cookies() {
|
|
|
|
|
r.cookies[v.Name] = v.Value
|
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
|
}
|
2020-03-19 22:56:12 +08:00
|
|
|
}
|
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// GetCookie retrieves and returns the cookie value of specified `key`.
|
2021-01-25 14:54:38 +08:00
|
|
|
func (r *Response) GetCookie(key string) string {
|
2020-03-19 22:56:12 +08:00
|
|
|
r.initCookie()
|
2019-06-19 09:06:52 +08:00
|
|
|
return r.cookies[key]
|
2018-01-02 16:35:13 +08:00
|
|
|
}
|
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
// GetCookieMap retrieves and returns a copy of current cookie values map.
|
2021-01-25 14:54:38 +08:00
|
|
|
func (r *Response) GetCookieMap() map[string]string {
|
2020-03-19 22:56:12 +08:00
|
|
|
r.initCookie()
|
|
|
|
|
m := make(map[string]string, len(r.cookies))
|
|
|
|
|
for k, v := range r.cookies {
|
|
|
|
|
m[k] = v
|
|
|
|
|
}
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-11 10:00:10 +08:00
|
|
|
// ReadAll retrieves and returns the response content as []byte.
|
2021-01-25 14:54:38 +08:00
|
|
|
func (r *Response) ReadAll() []byte {
|
2021-02-01 21:51:42 +08:00
|
|
|
// Response might be nil.
|
|
|
|
|
if r == nil || r.Response == nil {
|
|
|
|
|
return []byte{}
|
|
|
|
|
}
|
2025-08-22 13:29:09 +08:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2019-06-19 09:06:52 +08:00
|
|
|
if err != nil {
|
2022-01-28 14:51:49 +08:00
|
|
|
intlog.Errorf(r.request.Context(), `%+v`, err)
|
2019-06-19 09:06:52 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return body
|
2017-12-07 14:57:16 +08:00
|
|
|
}
|
|
|
|
|
|
2020-02-11 10:00:10 +08:00
|
|
|
// ReadAllString retrieves and returns the response content as string.
|
2021-01-25 14:54:38 +08:00
|
|
|
func (r *Response) ReadAllString() string {
|
2021-09-14 19:30:20 +08:00
|
|
|
return string(r.ReadAll())
|
2019-02-17 20:45:35 +08:00
|
|
|
}
|
|
|
|
|
|
2022-06-17 11:31:32 +08:00
|
|
|
// SetBodyContent overwrites response content with custom one.
|
|
|
|
|
func (r *Response) SetBodyContent(content []byte) {
|
|
|
|
|
buffer := bytes.NewBuffer(content)
|
2023-08-01 21:15:28 +08:00
|
|
|
r.Body = io.NopCloser(buffer)
|
2022-06-17 11:31:32 +08:00
|
|
|
r.ContentLength = int64(buffer.Len())
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-11 10:00:10 +08:00
|
|
|
// Close closes the response when it will never be used.
|
2021-01-25 14:54:38 +08:00
|
|
|
func (r *Response) Close() error {
|
2022-05-12 23:22:30 +08:00
|
|
|
if r == nil || r.Response == nil {
|
2020-07-25 11:24:35 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
2025-08-22 13:29:09 +08:00
|
|
|
return r.Body.Close()
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|