Skip to content

An error occurred when the url variable parameter was used with c.bind(slice) #1638

Closed
@elissa2333

Description

@elissa2333

Issue Description

An error occurred when the url variable parameter was used with c.bind(slice)

Checklist

  • Dependencies installed
  • No typos
  • Searched existing issues and docs

Expected behaviour

The binding result is consistent with the fixed url

Actual behaviour

raise error: binding element must be a struct

Steps to reproduce

pass: curl -X POST -H "Content-Type: application/json" -d '[{"id":2333}]' localhost:1323/a
fail: curl -X POST -H "Content-Type: application/json" -d '[{"id":2333}]' localhost:1323/b

Working code to debug

package main

import (
	"github.com/labstack/echo/v4"
	"net/http"
)

type Test struct {
	ID int `json:"id"`
}

func main() {
	e := echo.New()

	e.POST("/a", handle)

	// :name = b
	e.POST("/:name", handle) // -> error: binding element must be a struct
	e.Logger.Fatal(e.Start(":1323"))
}

func handle(c echo.Context) error {
	var m []*Test
	if err := c.Bind(&m); err != nil {
		return c.JSON(http.StatusBadRequest, echo.Map{"error": err})
	}

	return c.JSON(http.StatusOK, m)
}

Version/commit

github.com/labstack/echo/v4 v4.1.17

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions