ghttp_client.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 ghttp
  7. import (
  8. "github.com/gogf/gf/container/gvar"
  9. "github.com/gogf/gf/net/ghttp/internal/client"
  10. )
  11. type (
  12. Client = client.Client
  13. ClientResponse = client.Response
  14. ClientHandlerFunc = client.HandlerFunc
  15. )
  16. // NewClient creates and returns a new HTTP client object.
  17. func NewClient() *Client {
  18. return client.New()
  19. }
  20. // Get is a convenience method for sending GET request.
  21. // NOTE that remembers CLOSING the response object when it'll never be used.
  22. // Deprecated, please use g.Client().Get or NewClient().Get instead.
  23. func Get(url string, data ...interface{}) (*ClientResponse, error) {
  24. return DoRequest("GET", url, data...)
  25. }
  26. // Put is a convenience method for sending PUT request.
  27. // NOTE that remembers CLOSING the response object when it'll never be used.
  28. // Deprecated, please use g.Client().Put or NewClient().Put instead.
  29. func Put(url string, data ...interface{}) (*ClientResponse, error) {
  30. return DoRequest("PUT", url, data...)
  31. }
  32. // Post is a convenience method for sending POST request.
  33. // NOTE that remembers CLOSING the response object when it'll never be used.
  34. // Deprecated, please use g.Client().Post or NewClient().Post instead.
  35. func Post(url string, data ...interface{}) (*ClientResponse, error) {
  36. return DoRequest("POST", url, data...)
  37. }
  38. // Delete is a convenience method for sending DELETE request.
  39. // NOTE that remembers CLOSING the response object when it'll never be used.
  40. // Deprecated, please use g.Client().Delete or NewClient().Delete instead.
  41. func Delete(url string, data ...interface{}) (*ClientResponse, error) {
  42. return DoRequest("DELETE", url, data...)
  43. }
  44. // Head is a convenience method for sending HEAD request.
  45. // NOTE that remembers CLOSING the response object when it'll never be used.
  46. // Deprecated, please use g.Client().Head or NewClient().Head instead.
  47. func Head(url string, data ...interface{}) (*ClientResponse, error) {
  48. return DoRequest("HEAD", url, data...)
  49. }
  50. // Patch is a convenience method for sending PATCH request.
  51. // NOTE that remembers CLOSING the response object when it'll never be used.
  52. // Deprecated, please use g.Client().Patch or NewClient().Patch instead.
  53. func Patch(url string, data ...interface{}) (*ClientResponse, error) {
  54. return DoRequest("PATCH", url, data...)
  55. }
  56. // Connect is a convenience method for sending CONNECT request.
  57. // NOTE that remembers CLOSING the response object when it'll never be used.
  58. // Deprecated, please use g.Client().Connect or NewClient().Connect instead.
  59. func Connect(url string, data ...interface{}) (*ClientResponse, error) {
  60. return DoRequest("CONNECT", url, data...)
  61. }
  62. // Options is a convenience method for sending OPTIONS request.
  63. // NOTE that remembers CLOSING the response object when it'll never be used.
  64. // Deprecated, please use g.Client().Options or NewClient().Options instead.
  65. func Options(url string, data ...interface{}) (*ClientResponse, error) {
  66. return DoRequest("OPTIONS", url, data...)
  67. }
  68. // Trace is a convenience method for sending TRACE request.
  69. // NOTE that remembers CLOSING the response object when it'll never be used.
  70. // Deprecated, please use g.Client().Trace or NewClient().Trace instead.
  71. func Trace(url string, data ...interface{}) (*ClientResponse, error) {
  72. return DoRequest("TRACE", url, data...)
  73. }
  74. // DoRequest is a convenience method for sending custom http method request.
  75. // NOTE that remembers CLOSING the response object when it'll never be used.
  76. // Deprecated, please use g.Client().DoRequest or NewClient().DoRequest instead.
  77. func DoRequest(method, url string, data ...interface{}) (*ClientResponse, error) {
  78. return client.New().DoRequest(method, url, data...)
  79. }
  80. // GetContent is a convenience method for sending GET request, which retrieves and returns
  81. // the result content and automatically closes response object.
  82. // Deprecated, please use g.Client().GetContent or NewClient().GetContent instead.
  83. func GetContent(url string, data ...interface{}) string {
  84. return RequestContent("GET", url, data...)
  85. }
  86. // PutContent is a convenience method for sending PUT request, which retrieves and returns
  87. // the result content and automatically closes response object.
  88. // Deprecated, please use g.Client().PutContent or NewClient().PutContent instead.
  89. func PutContent(url string, data ...interface{}) string {
  90. return RequestContent("PUT", url, data...)
  91. }
  92. // PostContent is a convenience method for sending POST request, which retrieves and returns
  93. // the result content and automatically closes response object.
  94. // Deprecated, please use g.Client().PostContent or NewClient().PostContent instead.
  95. func PostContent(url string, data ...interface{}) string {
  96. return RequestContent("POST", url, data...)
  97. }
  98. // DeleteContent is a convenience method for sending DELETE request, which retrieves and returns
  99. // the result content and automatically closes response object.
  100. // Deprecated, please use g.Client().DeleteContent or NewClient().DeleteContent instead.
  101. func DeleteContent(url string, data ...interface{}) string {
  102. return RequestContent("DELETE", url, data...)
  103. }
  104. // HeadContent is a convenience method for sending HEAD request, which retrieves and returns
  105. // the result content and automatically closes response object.
  106. // Deprecated, please use g.Client().HeadContent or NewClient().HeadContent instead.
  107. func HeadContent(url string, data ...interface{}) string {
  108. return RequestContent("HEAD", url, data...)
  109. }
  110. // PatchContent is a convenience method for sending PATCH request, which retrieves and returns
  111. // the result content and automatically closes response object.
  112. // Deprecated, please use g.Client().PatchContent or NewClient().PatchContent instead.
  113. func PatchContent(url string, data ...interface{}) string {
  114. return RequestContent("PATCH", url, data...)
  115. }
  116. // ConnectContent is a convenience method for sending CONNECT request, which retrieves and returns
  117. // the result content and automatically closes response object.
  118. // Deprecated, please use g.Client().ConnectContent or NewClient().ConnectContent instead.
  119. func ConnectContent(url string, data ...interface{}) string {
  120. return RequestContent("CONNECT", url, data...)
  121. }
  122. // OptionsContent is a convenience method for sending OPTIONS request, which retrieves and returns
  123. // the result content and automatically closes response object.
  124. // Deprecated, please use g.Client().OptionsContent or NewClient().OptionsContent instead.
  125. func OptionsContent(url string, data ...interface{}) string {
  126. return RequestContent("OPTIONS", url, data...)
  127. }
  128. // TraceContent is a convenience method for sending TRACE request, which retrieves and returns
  129. // the result content and automatically closes response object.
  130. // Deprecated, please use g.Client().TraceContent or NewClient().TraceContent instead.
  131. func TraceContent(url string, data ...interface{}) string {
  132. return RequestContent("TRACE", url, data...)
  133. }
  134. // RequestContent is a convenience method for sending custom http method request, which
  135. // retrieves and returns the result content and automatically closes response object.
  136. // Deprecated, please use g.Client().RequestContent or NewClient().RequestContent instead.
  137. func RequestContent(method string, url string, data ...interface{}) string {
  138. return client.New().RequestContent(method, url, data...)
  139. }
  140. // GetBytes is a convenience method for sending GET request, which retrieves and returns
  141. // the result content as bytes and automatically closes response object.
  142. // Deprecated, please use g.Client().GetBytes or NewClient().GetBytes instead.
  143. func GetBytes(url string, data ...interface{}) []byte {
  144. return RequestBytes("GET", url, data...)
  145. }
  146. // PutBytes is a convenience method for sending PUT request, which retrieves and returns
  147. // the result content as bytes and automatically closes response object.
  148. // Deprecated, please use g.Client().PutBytes or NewClient().PutBytes instead.
  149. func PutBytes(url string, data ...interface{}) []byte {
  150. return RequestBytes("PUT", url, data...)
  151. }
  152. // PostBytes is a convenience method for sending POST request, which retrieves and returns
  153. // the result content as bytes and automatically closes response object.
  154. // Deprecated, please use g.Client().PostBytes or NewClient().PostBytes instead.
  155. func PostBytes(url string, data ...interface{}) []byte {
  156. return RequestBytes("POST", url, data...)
  157. }
  158. // DeleteBytes is a convenience method for sending DELETE request, which retrieves and returns
  159. // the result content as bytes and automatically closes response object.
  160. // Deprecated, please use g.Client().DeleteBytes or NewClient().DeleteBytes instead.
  161. func DeleteBytes(url string, data ...interface{}) []byte {
  162. return RequestBytes("DELETE", url, data...)
  163. }
  164. // HeadBytes is a convenience method for sending HEAD request, which retrieves and returns
  165. // the result content as bytes and automatically closes response object.
  166. // Deprecated, please use g.Client().HeadBytes or NewClient().HeadBytes instead.
  167. func HeadBytes(url string, data ...interface{}) []byte {
  168. return RequestBytes("HEAD", url, data...)
  169. }
  170. // PatchBytes is a convenience method for sending PATCH request, which retrieves and returns
  171. // the result content as bytes and automatically closes response object.
  172. // Deprecated, please use g.Client().PatchBytes or NewClient().PatchBytes instead.
  173. func PatchBytes(url string, data ...interface{}) []byte {
  174. return RequestBytes("PATCH", url, data...)
  175. }
  176. // ConnectBytes is a convenience method for sending CONNECT request, which retrieves and returns
  177. // the result content as bytes and automatically closes response object.
  178. // Deprecated, please use g.Client().ConnectBytes or NewClient().ConnectBytes instead.
  179. func ConnectBytes(url string, data ...interface{}) []byte {
  180. return RequestBytes("CONNECT", url, data...)
  181. }
  182. // OptionsBytes is a convenience method for sending OPTIONS request, which retrieves and returns
  183. // the result content as bytes and automatically closes response object.
  184. // Deprecated, please use g.Client().OptionsBytes or NewClient().OptionsBytes instead.
  185. func OptionsBytes(url string, data ...interface{}) []byte {
  186. return RequestBytes("OPTIONS", url, data...)
  187. }
  188. // TraceBytes is a convenience method for sending TRACE request, which retrieves and returns
  189. // the result content as bytes and automatically closes response object.
  190. // Deprecated, please use g.Client().TraceBytes or NewClient().TraceBytes instead.
  191. func TraceBytes(url string, data ...interface{}) []byte {
  192. return RequestBytes("TRACE", url, data...)
  193. }
  194. // RequestBytes is a convenience method for sending custom http method request, which
  195. // retrieves and returns the result content as bytes and automatically closes response object.
  196. // Deprecated, please use g.Client().RequestBytes or NewClient().RequestBytes instead.
  197. func RequestBytes(method string, url string, data ...interface{}) []byte {
  198. return client.New().RequestBytes(method, url, data...)
  199. }
  200. // GetVar sends a GET request, retrieves and converts the result content to specified pointer.
  201. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
  202. // Deprecated, please use g.Client().GetVar or NewClient().GetVar instead.
  203. func GetVar(url string, data ...interface{}) *gvar.Var {
  204. return RequestVar("GET", url, data...)
  205. }
  206. // PutVar sends a PUT request, retrieves and converts the result content to specified pointer.
  207. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
  208. // Deprecated, please use g.Client().PutVar or NewClient().PutVar instead.
  209. func PutVar(url string, data ...interface{}) *gvar.Var {
  210. return RequestVar("PUT", url, data...)
  211. }
  212. // PostVar sends a POST request, retrieves and converts the result content to specified pointer.
  213. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
  214. // Deprecated, please use g.Client().PostVar or NewClient().PostVar instead.
  215. func PostVar(url string, data ...interface{}) *gvar.Var {
  216. return RequestVar("POST", url, data...)
  217. }
  218. // DeleteVar sends a DELETE request, retrieves and converts the result content to specified pointer.
  219. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
  220. // Deprecated, please use g.Client().DeleteVar or NewClient().DeleteVar instead.
  221. func DeleteVar(url string, data ...interface{}) *gvar.Var {
  222. return RequestVar("DELETE", url, data...)
  223. }
  224. // HeadVar sends a HEAD request, retrieves and converts the result content to specified pointer.
  225. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
  226. // Deprecated, please use g.Client().HeadVar or NewClient().HeadVar instead.
  227. func HeadVar(url string, data ...interface{}) *gvar.Var {
  228. return RequestVar("HEAD", url, data...)
  229. }
  230. // PatchVar sends a PATCH request, retrieves and converts the result content to specified pointer.
  231. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
  232. // Deprecated, please use g.Client().PatchVar or NewClient().PatchVar instead.
  233. func PatchVar(url string, data ...interface{}) *gvar.Var {
  234. return RequestVar("PATCH", url, data...)
  235. }
  236. // ConnectVar sends a CONNECT request, retrieves and converts the result content to specified pointer.
  237. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
  238. // Deprecated, please use g.Client().ConnectVar or NewClient().ConnectVar instead.
  239. func ConnectVar(url string, data ...interface{}) *gvar.Var {
  240. return RequestVar("CONNECT", url, data...)
  241. }
  242. // OptionsVar sends a OPTIONS request, retrieves and converts the result content to specified pointer.
  243. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
  244. // Deprecated, please use g.Client().OptionsVar or NewClient().OptionsVar instead.
  245. func OptionsVar(url string, data ...interface{}) *gvar.Var {
  246. return RequestVar("OPTIONS", url, data...)
  247. }
  248. // TraceVar sends a TRACE request, retrieves and converts the result content to specified pointer.
  249. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
  250. // Deprecated, please use g.Client().TraceVar or NewClient().TraceVar instead.
  251. func TraceVar(url string, data ...interface{}) *gvar.Var {
  252. return RequestVar("TRACE", url, data...)
  253. }
  254. // RequestVar sends request using given HTTP method and data, retrieves converts the result
  255. // to specified pointer. It reads and closes the response object internally automatically.
  256. // The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, et
  257. // Deprecated, please use g.Client().RequestVar or NewClient().RequestVar instead.
  258. func RequestVar(method string, url string, data ...interface{}) *gvar.Var {
  259. response, err := DoRequest(method, url, data...)
  260. if err != nil {
  261. return gvar.New(nil)
  262. }
  263. defer response.Close()
  264. return gvar.New(response.ReadAll())
  265. }