ghttp_client_var.go 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. import (
  8. "github.com/gogf/gf/container/gvar"
  9. )
  10. // GetVar sends a GET request, retrieves and converts the result content to specified pointer.
  11. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  12. func (c *Client) GetVar(url string, data ...interface{}) *gvar.Var {
  13. return c.RequestVar("GET", url, data...)
  14. }
  15. // PutVar sends a PUT request, retrieves and converts the result content to specified pointer.
  16. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  17. func (c *Client) PutVar(url string, data ...interface{}) *gvar.Var {
  18. return c.RequestVar("PUT", url, data...)
  19. }
  20. // PostVar sends a POST request, retrieves and converts the result content to specified pointer.
  21. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  22. func (c *Client) PostVar(url string, data ...interface{}) *gvar.Var {
  23. return c.RequestVar("POST", url, data...)
  24. }
  25. // DeleteVar sends a DELETE request, retrieves and converts the result content to specified pointer.
  26. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  27. func (c *Client) DeleteVar(url string, data ...interface{}) *gvar.Var {
  28. return c.RequestVar("DELETE", url, data...)
  29. }
  30. // HeadVar sends a HEAD request, retrieves and converts the result content to specified pointer.
  31. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  32. func (c *Client) HeadVar(url string, data ...interface{}) *gvar.Var {
  33. return c.RequestVar("HEAD", url, data...)
  34. }
  35. // PatchVar sends a PATCH request, retrieves and converts the result content to specified pointer.
  36. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  37. func (c *Client) PatchVar(url string, data ...interface{}) *gvar.Var {
  38. return c.RequestVar("PATCH", url, data...)
  39. }
  40. // ConnectVar sends a CONNECT request, retrieves and converts the result content to specified pointer.
  41. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  42. func (c *Client) ConnectVar(url string, data ...interface{}) *gvar.Var {
  43. return c.RequestVar("CONNECT", url, data...)
  44. }
  45. // OptionsVar sends a OPTIONS request, retrieves and converts the result content to specified pointer.
  46. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  47. func (c *Client) OptionsVar(url string, data ...interface{}) *gvar.Var {
  48. return c.RequestVar("OPTIONS", url, data...)
  49. }
  50. // TraceVar sends a TRACE request, retrieves and converts the result content to specified pointer.
  51. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  52. func (c *Client) TraceVar(url string, data ...interface{}) *gvar.Var {
  53. return c.RequestVar("TRACE", url, data...)
  54. }
  55. // RequestVar sends request using given HTTP method and data, retrieves converts the result
  56. // to specified pointer. It reads and closes the response object internally automatically.
  57. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
  58. func (c *Client) RequestVar(method string, url string, data ...interface{}) *gvar.Var {
  59. response, err := c.DoRequest(method, url, data...)
  60. if err != nil {
  61. return gvar.New(nil)
  62. }
  63. defer response.Close()
  64. return gvar.New(response.ReadAll())
  65. }