labstack / echo

High performance, minimalist Go web framework

Home Page:https://echo.labstack.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Omitempty in form

SimonAM opened this issue · comments

Issue Description

Cannot bind form-data with omitempty tag.

Checklist

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

Expected behaviour

Should bind form data with omitempty tag if the data exists

Actual behaviour

Field with omitempty tag does not get bound.

Steps to reproduce

See working code example below:

Working code to debug

package main

import (
	"net/http"

	"github.com/labstack/echo/v4"
)

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

	e.POST("/issue", PostExample)

	e.Logger.Fatal(e.Start(":8080"))
}

type FormStruct struct {
	MyField    string `form:"myField,omitempty" json:"myField"`
	OtherField string `form:"otherField" json:"otherField"`
}

func PostExample(c echo.Context) error {
	var formData FormStruct
	_ = c.Bind(&formData)
	return c.JSON(http.StatusOK, formData)
}

Edit:
And curl:
curl -X POST http://localhost:8080/issue \ -d "myField=myFieldData" \ -d "otherField=otherFieldData" \ -H "Content-Type: application/x-www-form-urlencoded"

Version/commit

omitempty works only for json and xml sources as these use Go Std marshalling. form,query, param do not support it.