Bind to custom type? #2646
StephanSchmidt
started this conversation in
General
Replies: 1 comment 2 replies
-
Have you tried? package main
import (
"errors"
"github.com/labstack/echo/v4"
"log/slog"
"net/http"
)
type PathId string
type Payload struct {
Path PathId `param:"path_id"`
}
func main() {
e := echo.New()
e.GET("/:path_id", func(c echo.Context) error {
payload := Payload{}
if err := c.Bind(&payload); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
return c.JSON(http.StatusOK, payload)
})
if err := e.Start(":8080"); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("server closed due to unexpected error", "err", err)
}
} Example: x@x:~/code/echo$ curl http://localhost:8080/xxx
{"Path":"xxx"} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
How would I bind to a custom type?
type PathId string
(string for simplicity, I do want to do some conversions)
Beta Was this translation helpful? Give feedback.
All reactions