gclient_bytes.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gclient
  7. import (
  8. "context"
  9. "net/http"
  10. "github.com/gogf/gf/v2/internal/intlog"
  11. )
  12. // GetBytes sends a GET request, retrieves and returns the result content as bytes.
  13. func (c *Client) GetBytes(ctx context.Context, url string, data ...interface{}) []byte {
  14. return c.RequestBytes(ctx, http.MethodGet, url, data...)
  15. }
  16. // PutBytes sends a PUT request, retrieves and returns the result content as bytes.
  17. func (c *Client) PutBytes(ctx context.Context, url string, data ...interface{}) []byte {
  18. return c.RequestBytes(ctx, http.MethodPut, url, data...)
  19. }
  20. // PostBytes sends a POST request, retrieves and returns the result content as bytes.
  21. func (c *Client) PostBytes(ctx context.Context, url string, data ...interface{}) []byte {
  22. return c.RequestBytes(ctx, http.MethodPost, url, data...)
  23. }
  24. // DeleteBytes sends a DELETE request, retrieves and returns the result content as bytes.
  25. func (c *Client) DeleteBytes(ctx context.Context, url string, data ...interface{}) []byte {
  26. return c.RequestBytes(ctx, http.MethodDelete, url, data...)
  27. }
  28. // HeadBytes sends a HEAD request, retrieves and returns the result content as bytes.
  29. func (c *Client) HeadBytes(ctx context.Context, url string, data ...interface{}) []byte {
  30. return c.RequestBytes(ctx, http.MethodHead, url, data...)
  31. }
  32. // PatchBytes sends a PATCH request, retrieves and returns the result content as bytes.
  33. func (c *Client) PatchBytes(ctx context.Context, url string, data ...interface{}) []byte {
  34. return c.RequestBytes(ctx, http.MethodPatch, url, data...)
  35. }
  36. // ConnectBytes sends a CONNECT request, retrieves and returns the result content as bytes.
  37. func (c *Client) ConnectBytes(ctx context.Context, url string, data ...interface{}) []byte {
  38. return c.RequestBytes(ctx, http.MethodConnect, url, data...)
  39. }
  40. // OptionsBytes sends a OPTIONS request, retrieves and returns the result content as bytes.
  41. func (c *Client) OptionsBytes(ctx context.Context, url string, data ...interface{}) []byte {
  42. return c.RequestBytes(ctx, http.MethodOptions, url, data...)
  43. }
  44. // TraceBytes sends a TRACE request, retrieves and returns the result content as bytes.
  45. func (c *Client) TraceBytes(ctx context.Context, url string, data ...interface{}) []byte {
  46. return c.RequestBytes(ctx, http.MethodTrace, url, data...)
  47. }
  48. // RequestBytes sends request using given HTTP method and data, retrieves returns the result
  49. // as bytes. It reads and closes the response object internally automatically.
  50. func (c *Client) RequestBytes(ctx context.Context, method string, url string, data ...interface{}) []byte {
  51. response, err := c.DoRequest(ctx, method, url, data...)
  52. if err != nil {
  53. intlog.Errorf(ctx, `%+v`, err)
  54. return nil
  55. }
  56. defer func() {
  57. if err = response.Close(); err != nil {
  58. intlog.Errorf(ctx, `%+v`, err)
  59. }
  60. }()
  61. return response.ReadAll()
  62. }