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

having trouble writing a customer logger middleware #782

Closed
atbaig opened this issue Dec 21, 2016 · 3 comments
Closed

having trouble writing a customer logger middleware #782

atbaig opened this issue Dec 21, 2016 · 3 comments

Comments

@atbaig
Copy link

atbaig commented Dec 21, 2016

Description

I am trying to log some custom fields in my custom logger middleware. I took the code from middleware/logger.go and tried to modify it in my own repo. I am getting EOF after I do a c.Bind()

Expected behaviour

I am expecting it work normally

Actual behaviour

I get an EOF after I add the c.Bind()

Steps to reproduce

middleware/logger.go

Working code to debug

return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
//Begin Custom Code
r1 := &Request{}
if err = c.Bind(&r1); err != nil {
fmt.Println(err)
return err
}
//End Custom code
req := c.Request()
res := c.Response()
start := time.Now()
if err = next(c); err != nil {
c.Error(err) //This is where I get an EOF. If I remove the custom code everything works perfectly
}
Am I doing something wrong?

package main

func main() {
}

Version/commit

@mbn18
Copy link

mbn18 commented Jul 23, 2017

Update

In my case the problem was caused by middleware that also used bind. Seems bind cannot be called more than once.
Any reason it is not accessible after first use?

Original problem

Have related issue

In one of my PUT request I get EOF

The request object is:

type RecommendationAddRequest struct {
	Xyz string `json:"xyz" validate:"required,gte=6"`
}

The binder is the default one:

	if err := c.Bind(reqObj); err != nil {
		println("error is:", err.Error())
		return false
	}

And it print out:

error is: code=400, message=EOF

The request is:

curl -X PUT \
  https://domain.com/api/whatever \
  -H 'authorization: Bearer some-string' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
	"xyz": "ssss"
}'

Tried to switch to POST and etc. The odd thing is that I have other POST/PUT actions that do work as expected

@vishr
Copy link
Member

vishr commented Jul 24, 2017

Any reason it is not accessible after first use?

Bind() reads request body directly from the socket and once read it can't read again, hence the EOF error. You can look into this blog post as a workaround: https://medium.com/@xoen/golang-read-from-an-io-readwriter-without-loosing-its-content-2c6911805361

@arthurlataks
Copy link

@vishr Isn't it possible to restore body content during the Bind or should I implement custom Binder for that purpose? For now I implemented body data restore like it is done in the Body Dump MW (and well, like in the Medium post), which makes me think that this should be handled in the MW?

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

4 participants