gclient_content.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. )
  11. // GetContent is a convenience method for sending GET request, which retrieves and returns
  12. // the result content and automatically closes response object.
  13. func (c *Client) GetContent(ctx context.Context, url string, data ...interface{}) string {
  14. return string(c.RequestBytes(ctx, http.MethodGet, url, data...))
  15. }
  16. // PutContent is a convenience method for sending PUT request, which retrieves and returns
  17. // the result content and automatically closes response object.
  18. func (c *Client) PutContent(ctx context.Context, url string, data ...interface{}) string {
  19. return string(c.RequestBytes(ctx, http.MethodPut, url, data...))
  20. }
  21. // PostContent is a convenience method for sending POST request, which retrieves and returns
  22. // the result content and automatically closes response object.
  23. func (c *Client) PostContent(ctx context.Context, url string, data ...interface{}) string {
  24. return string(c.RequestBytes(ctx, http.MethodPost, url, data...))
  25. }
  26. // DeleteContent is a convenience method for sending DELETE request, which retrieves and returns
  27. // the result content and automatically closes response object.
  28. func (c *Client) DeleteContent(ctx context.Context, url string, data ...interface{}) string {
  29. return string(c.RequestBytes(ctx, http.MethodDelete, url, data...))
  30. }
  31. // HeadContent is a convenience method for sending HEAD request, which retrieves and returns
  32. // the result content and automatically closes response object.
  33. func (c *Client) HeadContent(ctx context.Context, url string, data ...interface{}) string {
  34. return string(c.RequestBytes(ctx, http.MethodHead, url, data...))
  35. }
  36. // PatchContent is a convenience method for sending PATCH request, which retrieves and returns
  37. // the result content and automatically closes response object.
  38. func (c *Client) PatchContent(ctx context.Context, url string, data ...interface{}) string {
  39. return string(c.RequestBytes(ctx, http.MethodPatch, url, data...))
  40. }
  41. // ConnectContent is a convenience method for sending CONNECT request, which retrieves and returns
  42. // the result content and automatically closes response object.
  43. func (c *Client) ConnectContent(ctx context.Context, url string, data ...interface{}) string {
  44. return string(c.RequestBytes(ctx, http.MethodConnect, url, data...))
  45. }
  46. // OptionsContent is a convenience method for sending OPTIONS request, which retrieves and returns
  47. // the result content and automatically closes response object.
  48. func (c *Client) OptionsContent(ctx context.Context, url string, data ...interface{}) string {
  49. return string(c.RequestBytes(ctx, http.MethodOptions, url, data...))
  50. }
  51. // TraceContent is a convenience method for sending TRACE request, which retrieves and returns
  52. // the result content and automatically closes response object.
  53. func (c *Client) TraceContent(ctx context.Context, url string, data ...interface{}) string {
  54. return string(c.RequestBytes(ctx, http.MethodTrace, url, data...))
  55. }
  56. // RequestContent is a convenience method for sending custom http method request, which
  57. // retrieves and returns the result content and automatically closes response object.
  58. func (c *Client) RequestContent(ctx context.Context, method string, url string, data ...interface{}) string {
  59. return string(c.RequestBytes(ctx, method, url, data...))
  60. }