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

Request Body Logging failed #28

Open
fiurthorn opened this issue Sep 30, 2022 · 4 comments
Open

Request Body Logging failed #28

fiurthorn opened this issue Sep 30, 2022 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@fiurthorn
Copy link

Logging request body doesn't work for me because the body was already read at the time of logging.
So the body should be read before other handlers can read it.

like:

b, _ := io.ReadAll(r.Body)
r.Body = io.NopCloser(bytes.NewBuffer(b))
...
param := LogFormatterParams{
    Request:     r,
    RequestBody: b,
    isTerm:      isTerm,
}
@erudenko erudenko self-assigned this Sep 30, 2022
@erudenko erudenko added the bug Something isn't working label Sep 30, 2022
@erudenko
Copy link
Member

erudenko commented Mar 9, 2023

I believe it was an issue in early releases. Could you provide some code examples?

@fiurthorn
Copy link
Author

Always get an empty body

r = httplog.LoggerWithFormatter(httplog.FullFormatterWithRequestAndResponseHeadersAndBody)(r)
===
 EMPTY BODY 
===

@fiurthorn
Copy link
Author

first mistake httplog.FullFormatterWithRequestAndResponseHeadersAndBody do not set CaptureBodyto true.
But that makes no sens to me since it is named withBody.

second, I always have to capture the body before the

	middleware := func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			...
*			body, _ := io.ReadAll(r.Body)
*			r.Body = io.NopCloser(bytes.NewBuffer(bytes.Clone(body)))
			...
			wr := NewWriter(w, conf.CaptureBody)
			next.ServeHTTP(wr, r)
			...
			if !skip {
				r.Header = maskHeaderKeys(r.Header.Clone(), hideHeaderKeys)

				param := LogFormatterParams{
					Request:     r,
*					RequestBody: body,
					isTerm:      isTerm,
				}
			...

last but not least, that do not capture the response body from from the http.FileServer.

@fiurthorn
Copy link
Author

after some debuging, found that the ReadFrom method, do not capture the body, since it write to rw.ResponseWriter not to the own Writer. so the body will not captured.

func (rw *responseWriter) ReadFrom(r io.Reader) (n int64, err error) {
	if !rw.Written() {
		// The status will be StatusOK if WriteHeader has not been called yet
		rw.WriteHeader(http.StatusOK)
	}
*	n, err = io.Copy(rw.ResponseWriter, r)
	rw.size += int(n)
	return
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: In Progress
Development

No branches or pull requests

2 participants