2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-07-25 21:01:04 +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
|
2019-07-25 21:01:04 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
import (
|
|
|
|
"context"
|
2021-11-13 23:23:55 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/intlog"
|
2021-09-30 14:06:46 +08:00
|
|
|
)
|
|
|
|
|
2019-12-19 15:14:05 +08:00
|
|
|
// GetBytes sends a GET request, retrieves and returns the result content as bytes.
|
2021-09-30 14:06:46 +08:00
|
|
|
func (c *Client) GetBytes(ctx context.Context, url string, data ...interface{}) []byte {
|
2022-01-10 16:42:30 +08:00
|
|
|
return c.RequestBytes(ctx, httpMethodGet, url, data...)
|
2019-07-25 21:01:04 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 15:14:05 +08:00
|
|
|
// PutBytes sends a PUT request, retrieves and returns the result content as bytes.
|
2021-09-30 14:06:46 +08:00
|
|
|
func (c *Client) PutBytes(ctx context.Context, url string, data ...interface{}) []byte {
|
2022-01-10 16:42:30 +08:00
|
|
|
return c.RequestBytes(ctx, httpMethodPut, url, data...)
|
2019-07-25 21:01:04 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 15:14:05 +08:00
|
|
|
// PostBytes sends a POST request, retrieves and returns the result content as bytes.
|
2021-09-30 14:06:46 +08:00
|
|
|
func (c *Client) PostBytes(ctx context.Context, url string, data ...interface{}) []byte {
|
2022-01-10 16:42:30 +08:00
|
|
|
return c.RequestBytes(ctx, httpMethodPost, url, data...)
|
2019-07-25 21:01:04 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 15:14:05 +08:00
|
|
|
// DeleteBytes sends a DELETE request, retrieves and returns the result content as bytes.
|
2021-09-30 14:06:46 +08:00
|
|
|
func (c *Client) DeleteBytes(ctx context.Context, url string, data ...interface{}) []byte {
|
2022-01-10 16:42:30 +08:00
|
|
|
return c.RequestBytes(ctx, httpMethodDelete, url, data...)
|
2019-07-25 21:01:04 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 15:14:05 +08:00
|
|
|
// HeadBytes sends a HEAD request, retrieves and returns the result content as bytes.
|
2021-09-30 14:06:46 +08:00
|
|
|
func (c *Client) HeadBytes(ctx context.Context, url string, data ...interface{}) []byte {
|
2022-01-10 16:42:30 +08:00
|
|
|
return c.RequestBytes(ctx, httpMethodHead, url, data...)
|
2019-07-25 21:01:04 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 15:14:05 +08:00
|
|
|
// PatchBytes sends a PATCH request, retrieves and returns the result content as bytes.
|
2021-09-30 14:06:46 +08:00
|
|
|
func (c *Client) PatchBytes(ctx context.Context, url string, data ...interface{}) []byte {
|
2022-01-10 16:42:30 +08:00
|
|
|
return c.RequestBytes(ctx, httpMethodPatch, url, data...)
|
2019-07-25 21:01:04 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 15:14:05 +08:00
|
|
|
// ConnectBytes sends a CONNECT request, retrieves and returns the result content as bytes.
|
2021-09-30 14:06:46 +08:00
|
|
|
func (c *Client) ConnectBytes(ctx context.Context, url string, data ...interface{}) []byte {
|
2022-01-10 16:42:30 +08:00
|
|
|
return c.RequestBytes(ctx, httpMethodConnect, url, data...)
|
2019-07-25 21:01:04 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 15:14:05 +08:00
|
|
|
// OptionsBytes sends a OPTIONS request, retrieves and returns the result content as bytes.
|
2021-09-30 14:06:46 +08:00
|
|
|
func (c *Client) OptionsBytes(ctx context.Context, url string, data ...interface{}) []byte {
|
2022-01-10 16:42:30 +08:00
|
|
|
return c.RequestBytes(ctx, httpMethodOptions, url, data...)
|
2019-07-25 21:01:04 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 15:14:05 +08:00
|
|
|
// TraceBytes sends a TRACE request, retrieves and returns the result content as bytes.
|
2021-09-30 14:06:46 +08:00
|
|
|
func (c *Client) TraceBytes(ctx context.Context, url string, data ...interface{}) []byte {
|
2022-01-10 16:42:30 +08:00
|
|
|
return c.RequestBytes(ctx, httpMethodTrace, url, data...)
|
2019-07-25 21:01:04 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 15:14:05 +08:00
|
|
|
// RequestBytes sends request using given HTTP method and data, retrieves returns the result
|
|
|
|
// as bytes. It reads and closes the response object internally automatically.
|
2021-09-30 14:06:46 +08:00
|
|
|
func (c *Client) RequestBytes(ctx context.Context, method string, url string, data ...interface{}) []byte {
|
|
|
|
response, err := c.DoRequest(ctx, method, url, data...)
|
2019-07-25 21:01:04 +08:00
|
|
|
if err != nil {
|
2022-01-28 14:51:49 +08:00
|
|
|
intlog.Errorf(ctx, `%+v`, err)
|
2019-07-25 21:01:04 +08:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-30 14:06:46 +08:00
|
|
|
defer func() {
|
2021-12-21 22:59:14 +08:00
|
|
|
if err = response.Close(); err != nil {
|
2022-01-28 14:51:49 +08:00
|
|
|
intlog.Errorf(ctx, `%+v`, err)
|
2021-09-30 14:06:46 +08:00
|
|
|
}
|
|
|
|
}()
|
2019-07-25 21:01:04 +08:00
|
|
|
return response.ReadAll()
|
|
|
|
}
|