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"}