Comments (5)
No, it's been closed automatically if you don't read resp.Body
manually.
from req.
It's safe but may not work as expected due to the concurrency. Generally, client level setting should not call concurrently.
you can enable dump at request level, get the dump string and write it with your correlation id anywhere you want.
ref: https://req.cool/docs/tutorial/debugging/#dump-the-content
You can also enable dump at request level, which will not override the client-level dump setting, it will dump to the internal buffer and do not print to stdout by default, you can call Response.Dump() to get the dump result and print only if you want to, typically used in production, only record the content of the request when the request is abnormal to help us troubleshoot problems
from req.
Thanks imroc!
By the way, is resp.Body.Close() below still needed?
resp, err := httpClient.R().Get(url)
if err != nil {
this.Logger.Error(fmt.Sprintf("failed to fetch the page: %v", err))
return "", errors.New("httpClient.GetError")
}
defer resp.Body.Close() // is this still needed?
from req.
@imroc
should I close the resp.Body manually in the following code snippet?
func middlewareDumpReqAndResp(client *req.Client, resp *req.Response) error {
ctx := resp.Request.Context()
logger := logger.GetLoggerFromCtx(ctx)
respBody, err := io.ReadAll(io.Reader(resp.Body))
if err != nil {
err = errors.New(fmt.Sprint("get response payload body failed: ", err.Error()))
logger.Error(err.Error())
panic(err.Error())
}
resp.Body = io.NopCloser(io.Reader(bytes.NewBuffer(respBody)))
logger.Info(fmt.Sprintf("request url: %s \n request body: %s \n response body: %s", resp.Request.RawURL, string(resp.Request.Body), string(respBody)))
return nil
}
from req.
@imroc should I close the resp.Body manually in the following code snippet?
func middlewareDumpReqAndResp(client *req.Client, resp *req.Response) error { ctx := resp.Request.Context() logger := logger.GetLoggerFromCtx(ctx) respBody, err := io.ReadAll(io.Reader(resp.Body)) if err != nil { err = errors.New(fmt.Sprint("get response payload body failed: ", err.Error())) logger.Error(err.Error()) panic(err.Error()) } resp.Body = io.NopCloser(io.Reader(bytes.NewBuffer(respBody))) logger.Info(fmt.Sprintf("request url: %s \n request body: %s \n response body: %s", resp.Request.RawURL, string(resp.Request.Body), string(respBody))) return nil }
Unnecessary
from req.
Related Issues (20)
- ImpersonateFirefox - ECDSA verification failure HOT 2
- 看了文档之后,我知道了如何构建一个正常的请求,但如果我想构建这样的请求,请问应该怎么写才行。 HOT 2
- 向ChatGPT接口发送请求时,无法通过TLS指纹校验 HOT 2
- 关于cookie的疑问 HOT 4
- Does req support passing context from outside? HOT 2
- utls已经更新ECH的支持 ,能否同步更新一下 HOT 1
- Go 1.21 Support HOT 4
- 解gzip之后的render会panic HOT 2
- 我该怎么传递 binary 数据?
- ContentLength优化问题 HOT 2
- 使用dev模式如何导出 curl ? HOT 1
- Is the req.Client thread safe? HOT 1
- SetCookieJar + SetCookies
- 使用代理时代理服务器需要认证如何使用呢? HOT 2
- 上传与下载过程, 中途怎么取消呢? HOT 1
- 发送大量 HTTP 请求时,如何为每个请求单独指定代理? HOT 2
- Question about the `DisableForceMultipart` HOT 2
- ImpersonateChrome - tls: user canceled HOT 2
- 请问怎么设置请求头变量大写不转换成小写 HOT 2
- 使用h2协议,设置随机TLS指纹的时候出现问题:http2: unexpected ALPN protocol ""; want "h2" HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from req.