Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dump如何拿到完整的请求和响应报文? #363

Closed
1992w opened this issue Jun 26, 2024 · 6 comments
Closed

Dump如何拿到完整的请求和响应报文? #363

1992w opened this issue Jun 26, 2024 · 6 comments

Comments

@1992w
Copy link

1992w commented Jun 26, 2024

我在client设置了EnableDumpAllTo,我需要将每个请求完整的报文输出到指定的地方。但是目前测试的情况是按行来进行写入的,请问一下该怎么设置才能在每次写入时获得完整的报文?

@imroc
Copy link
Owner

imroc commented Jun 29, 2024

在请求级别设置dump

resp, err := client.R().EnableDump().Get(url)
...
content := resp.Dump()
...

@1992w
Copy link
Author

1992w commented Jun 30, 2024

后续会考虑在client级别加上吗? 在请求级别加我要加的地方太多了

@imroc
Copy link
Owner

imroc commented Jun 30, 2024

是指每个请求都写到不同文件吗?如果是client级别, 目前就是所有的请求都会写到同一个文件

@1992w
Copy link
Author

1992w commented Jul 1, 2024

是在client级别需要一个类似resp.Dump()的api。我目前在client通过EnableDumpAllTo设置了io.Writer,但是内部每次调用Write方法时传递的并不是完整的报文。我希望每次调用Write方法时都是完整的报文,这样我才好处理日志。

@imroc
Copy link
Owner

imroc commented Jul 1, 2024

是想单次write调用时写入完整报文是吧, 这个应该实现不了。
你实际可以利用response中间件来实现

	c := req.C().OnAfterResponse(func(client *req.Client, resp *req.Response) error {
    dump := resp.Dump()
    ...
  })

@1992w
Copy link
Author

1992w commented Jul 1, 2024

感谢,忘记中间件这回事了

@1992w 1992w closed this as completed Jul 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants