I am using Echo web framework in Golang and I wrote this code
package main
import (
"github.com/labstack/echo/v4"
"net/http"
)
type ProjectPath struct {
ID string `param:"id"`
}
type ProjectBody struct {
Name string `json:"name"`
}
func main() {
e := echo.New()
e.POST("/project/:id", getProjectHandler)
e.Start(":8080")
}
func getProjectHandler(c echo.Context) error {
path := new(ProjectPath)
if err := c.Bind(path); err != nil {
return err
}
body := new(ProjectBody)
if err := c.Bind(body); err != nil {
return err
}
// Access the fields separately
projectID := path.ID
projectName := body.Name
// Do something with the path and body fields...
// Return a response
return c.String(http.StatusOK, "Project ID: "+projectID+", Project Name: "+projectName)
}
I am trying to bind a path param and json body separately in a post request but I am getting an EOF error while trying to run it.
I am using replit for testing and the server is running on: https://echotest.sandeepacharya.repl.co
Curl Post Request:
curl -X POST https://echotest.sandeepacharya.repl.co/project/123 -H "Content-Type: application/json" -d '{"name":"Sandeep"}'
Response:
{"message":"EOF"}
2
Answers
https://github.com/labstack/echo/issues/782#issuecomment-317503513
The issue is occured due to that you are attempted to bind the request body twice. The second
c.Bind()
will raise an error because the request body has already been read and consumed.if you want to read the request with different struct, you can follow this approach
or you can use only one struct to bind the request
If you want to do the binding separately then you’ll need to use the source specific bind methods. These methods however are NOT available in the context, instead they are implemented by the
DefaultBinder
.See also: https://echo.labstack.com/guide/binding/#direct-source