Giter VIP home page Giter VIP logo

Comments (5)

imroc avatar imroc commented on June 12, 2024 1

No, it's been closed automatically if you don't read resp.Body manually.

from req.

imroc avatar imroc commented on June 12, 2024

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.

GingerMoon avatar GingerMoon commented on June 12, 2024

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.

GingerMoon avatar GingerMoon commented on June 12, 2024

@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 avatar imroc commented on June 12, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.