ghttp_client_bytes.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2017 gf Author(https://github.com/gogf/gf). 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 ghttp
  7. // GetBytes sends a GET request, retrieves and returns the result content as bytes.
  8. func (c *Client) GetBytes(url string, data ...interface{}) []byte {
  9. return c.RequestBytes("GET", url, data...)
  10. }
  11. // PutBytes sends a PUT request, retrieves and returns the result content as bytes.
  12. func (c *Client) PutBytes(url string, data ...interface{}) []byte {
  13. return c.RequestBytes("PUT", url, data...)
  14. }
  15. // PostBytes sends a POST request, retrieves and returns the result content as bytes.
  16. func (c *Client) PostBytes(url string, data ...interface{}) []byte {
  17. return c.RequestBytes("POST", url, data...)
  18. }
  19. // DeleteBytes sends a DELETE request, retrieves and returns the result content as bytes.
  20. func (c *Client) DeleteBytes(url string, data ...interface{}) []byte {
  21. return c.RequestBytes("DELETE", url, data...)
  22. }
  23. // HeadBytes sends a HEAD request, retrieves and returns the result content as bytes.
  24. func (c *Client) HeadBytes(url string, data ...interface{}) []byte {
  25. return c.RequestBytes("HEAD", url, data...)
  26. }
  27. // PatchBytes sends a PATCH request, retrieves and returns the result content as bytes.
  28. func (c *Client) PatchBytes(url string, data ...interface{}) []byte {
  29. return c.RequestBytes("PATCH", url, data...)
  30. }
  31. // ConnectBytes sends a CONNECT request, retrieves and returns the result content as bytes.
  32. func (c *Client) ConnectBytes(url string, data ...interface{}) []byte {
  33. return c.RequestBytes("CONNECT", url, data...)
  34. }
  35. // OptionsBytes sends a OPTIONS request, retrieves and returns the result content as bytes.
  36. func (c *Client) OptionsBytes(url string, data ...interface{}) []byte {
  37. return c.RequestBytes("OPTIONS", url, data...)
  38. }
  39. // TraceBytes sends a TRACE request, retrieves and returns the result content as bytes.
  40. func (c *Client) TraceBytes(url string, data ...interface{}) []byte {
  41. return c.RequestBytes("TRACE", url, data...)
  42. }
  43. // RequestBytes sends request using given HTTP method and data, retrieves returns the result
  44. // as bytes. It reads and closes the response object internally automatically.
  45. func (c *Client) RequestBytes(method string, url string, data ...interface{}) []byte {
  46. response, err := c.DoRequest(method, url, data...)
  47. if err != nil {
  48. return nil
  49. }
  50. defer response.Close()
  51. return response.ReadAll()
  52. }