工具方法

ghttp模块也提供了独立的包函数来实现HTTP请求,这是一些快捷方法,用于短连接请求,方法如下:

  1. func Get(url string, data ...interface{}) (*ClientResponse, error)
  2. func Put(url string, data ...interface{}) (*ClientResponse, error)
  3. func Post(url string, data ...interface{}) (*ClientResponse, error)
  4. func Delete(url string, data ...interface{}) (*ClientResponse, error)
  5. func Connect(url string, data ...interface{}) (*ClientResponse, error)
  6. func Head(url string, data ...interface{}) (*ClientResponse, error)
  7. func Options(url string, data ...interface{}) (*ClientResponse, error)
  8. func Patch(url string, data ...interface{}) (*ClientResponse, error)
  9. func Trace(url string, data ...interface{}) (*ClientResponse, error)
  10. func DoRequest(method, url string, data ...interface{}) (*ClientResponse, error)
  11. func GetBytes(url string, data ...interface{}) []byte
  12. func PutBytes(url string, data ...interface{}) []byte
  13. func PostBytes(url string, data ...interface{}) []byte
  14. func DeleteBytes(url string, data ...interface{}) []byte
  15. func ConnectBytes(url string, data ...interface{}) []byte
  16. func HeadBytes(url string, data ...interface{}) []byte
  17. func OptionsBytes(url string, data ...interface{}) []byte
  18. func PatchBytes(url string, data ...interface{}) []byte
  19. func TraceBytes(url string, data ...interface{}) []byte
  20. func DoRequestBytes(method string, url string, data ...interface{}) []byte
  21. func GetContent(url string, data ...interface{}) string
  22. func PutContent(url string, data ...interface{}) string
  23. func PostContent(url string, data ...interface{}) string
  24. func DeleteContent(url string, data ...interface{}) string
  25. func ConnectContent(url string, data ...interface{}) string
  26. func HeadContent(url string, data ...interface{}) string
  27. func OptionsContent(url string, data ...interface{}) string
  28. func PatchContent(url string, data ...interface{}) string
  29. func TraceContent(url string, data ...interface{}) string
  30. func DoRequestContent(method string, url string, data ...interface{}) string

函数说明与Client对象的方法说明一致,一般情况下直接使用ghttp对应的HTTP包方法来实现HTTP请求即可,非常简便。当需要自定义HTTP请求的一些细节(例如超时时间、CookieHeader等)时,就得依靠自定义的Client对象来处理了。