原始信息打印

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

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

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

基本使用

  1. package main
  2. import (
  3. "github.com/gogf/gf/frame/g"
  4. )
  5. func main() {
  6. response, err := g.Client().Get("https://goframe.org")
  7. if err != nil {
  8. panic(err)
  9. }
  10. response.RawDump()
  11. }

执行后,终端输出为:

  1. +---------------------------------------------+
  2. | REQUEST |
  3. +---------------------------------------------+
  4. GET / HTTP/1.1
  5. Host: goframe.org
  6. User-Agent: Go-http-client/1.1
  7. Accept-Encoding: gzip
  8. +---------------------------------------------+
  9. | RESPONSE |
  10. +---------------------------------------------+
  11. HTTP/1.1 200 OK
  12. Connection: close
  13. Transfer-Encoding: chunked
  14. Content-Type: text/html; charset=utf-8
  15. Date: Mon, 08 Jun 2020 11:33:26 GMT
  16. Server: nginx/1.10.3 (Ubuntu)
  17. <!DOCTYPE html>
  18. ...