Closed SimonAM closed 3 months ago
Cannot bind form-data with omitempty tag.
omitempty
Should bind form data with omitempty tag if the data exists
Field with omitempty tag does not get bound.
See working code example below:
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"
curl -X POST http://localhost:8080/issue \ -d "myField=myFieldData" \ -d "otherField=otherFieldData" \ -H "Content-Type: application/x-www-form-urlencoded"
omitempty works only for json and xml sources as these use Go Std marshalling. form,query, param do not support it.
json
xml
form
query
param
Issue Description
Cannot bind form-data with
omitempty
tag.Checklist
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
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