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

c.Bind failed when with a query param #1448

Closed
tadada opened this issue Nov 20, 2019 · 2 comments
Closed

c.Bind failed when with a query param #1448

tadada opened this issue Nov 20, 2019 · 2 comments
Labels

Comments

@tadada
Copy link

tadada commented Nov 20, 2019

c.Bind failed when with a query param

Checklist

  • [*] Dependencies installed
  • [*] No typos
  • [*] Searched existing issues and docs

Expected behaviour

Expected second curl cmd result
steps

Actual behaviour

First curl cmd result

Steps to reproduce

See code and curl cmd

Working code to debug

package main

import (
	"log"
	"net/http"
	"time"

	"github.com/labstack/echo"
)

type component struct {
	ID          string    `json:"id" gorm:"primary_key;unique_index"`
	ContainerID string    `json:"containerID" gorm:"not null"`
	Name        string    `json:"name" gorm:"not null;unique;size:255;index"`
	Value       string    `json:"value"`
	Order       int       `json:"order" gorm:"not null"`
	URL         string    `json:"url"`
	Username    string    `json:"username" gorm:"not null"`
	Type        string    `json:"type" gorm:"not null"`
	IsShow      bool      `json:"isShow"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

func main() {
	e := echo.New()
	e.PUT("/", func(c echo.Context) error {
		cs := []component{}
		if err := c.Bind(&cs); err != nil {
			log.Println("bind components err", err)
			return c.String(http.StatusOK, "Hello, NO!\n")
		}
		log.Println("No err")
		return c.String(http.StatusOK, "Hello, World!\n")
	})
	e.Logger.Fatal(e.Start(":1323"))
}

Faild:
curl -v 'http://localhost:1323?container_id=849779cd-b548-4d86-b516-ad056afed882' -X PUT  -H 'Content-Type: application/json;charset=UTF-8'  --data-binary '[{"containerID":"849779cd-b548-4d86-b516-ad056afed882","name":"xx","value":"jwt","order":3,"url":"9d008e8599f60f00df95c660ed9cf3edw9eMSAxLXuj","isShow":true,"type":"all"},{"containerID":"849779cd-b548-4d86-b516-ad056afed882","name":"xxx","value":"jwt","order":5,"url":"88efc1722a6ffaae635d071e3c8dc49dyVSXGDkOKoJ","isShow":true,"type":"all"}]'

Success:
curl -v 'http://localhost:1323' -X PUT  -H 'Content-Type: application/json;charset=UTF-8'  --data-binary '[{"containerID":"849779cd-b548-4d86-b516-ad056afed882","name":"统一门户","value":"jwt","order":3,"url":"9d008e8599f60f00df95c660ed9cf3edw9eMSAxLXuj","isShow":true,"type":"all"},{"containerID":"849779cd-b548-4d86-b516-ad056afed882","name":"股东报名","value":"jwt","order":5,"url":"88efc1722a6ffaae635d071e3c8dc49dyVSXGDkOKoJ","isShow":true,"type":"all"}]'

Version/commit

v4.1.11
echo-v

@nikolay-turpitko
Copy link

@tadada According to your code, you logged binding error into console. Just curious to look at it.

@stale
Copy link

stale bot commented Feb 20, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Feb 20, 2020
@stale stale bot closed this as completed Feb 27, 2020
benoitmasson added a commit to benoitmasson/echo that referenced this issue May 19, 2020
Do not throw an error when binding to a slice, if binding occurs
on path- or query-parameters
=> data is very likely to be found in the body, which will be bound later

Should fix labstack#1356, labstack#1448, labstack#1495 (second case), labstack#1565
benoitmasson added a commit to benoitmasson/echo that referenced this issue Aug 17, 2020
Do not throw an error when binding to a slice, if binding occurs
on path- or query-parameters
=> data is very likely to be found in the body, which will be bound later

Should fix labstack#1356, labstack#1448, labstack#1495 (second case), labstack#1565
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants