基本介绍

http 客户端支持对HTTP请求的输入与输出原始信息获取与打印,方便调试,相关方法如下:

  1. func (r *Response) Raw() string
  2. func (r *Response) RawDump()
  3. func (r *Response) RawRequest() string
  4. func (r *Response) RawResponse() string

可以看到,所有的方法均绑定在 Response 对象上,也就是说必须要请求结束后才能打印。

使用示例

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/os/gctx"
  5. )
  6. func main() {
  7. response, err := g.Client().Post(
  8. gctx.New(),
  9. "https://goframe.org",
  10. g.Map{
  11. "name": "john",
  12. },
  13. )
  14. if err != nil {
  15. panic(err)
  16. }
  17. response.RawDump()
  18. }

执行后,终端输出为:

  1. +---------------------------------------------+
  2. | REQUEST |
  3. +---------------------------------------------+
  4. POST / HTTP/1.1
  5. Host: goframe.org
  6. User-Agent: GoFrameHTTPClient v2.0.0-beta
  7. Content-Length: 9
  8. Content-Type: application/x-www-form-urlencoded
  9. Accept-Encoding: gzip
  10. name=john
  11. +---------------------------------------------+
  12. | RESPONSE |
  13. +---------------------------------------------+
  14. HTTP/1.1 405 Method Not Allowed
  15. Connection: close
  16. Transfer-Encoding: chunked
  17. Allow: GET
  18. Cache-Control: no-store
  19. Content-Security-Policy: frame-ancestors 'self'
  20. Content-Type: text/html;charset=UTF-8
  21. Date: Fri, 03 Dec 2021 09:43:29 GMT
  22. Expires: Thu, 01 Jan 1970 00:00:00 GMT
  23. Server: nginx
  24. ...