Skip to content

Dont return 400 from .Bind() in POST / PUT with no body #1409

Closed
@cousinbenson

Description

@cousinbenson

Issue Description

The default binder currently returns 400 if req.ContentLength == 0 during methods that aren't GET or DELETE. It is generally NOT regarded bad practice to make POST / PUT requests with an empty body. It should be up the resource to determine whether or not this should error and not the HTTP protocol.

#1214 was created with regards to this issue, but eventually went stale after no action was taken.

Some discussion on the topic can be found here:

Checklist

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

Expected behaviour

c.Bind() should not return 400 for POST / PUT with empty bodies

Actual behaviour

c.Bind() returns 400 for POST / PUT with empty bodies

Steps to reproduce

call c.Bind() in a POST handler with an empty body

Working code to debug

package main

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

func main() {
	srv := echo.New()
	srv.POST("/test", testPostBody)
	srv.Start(":1234")
}

type Params struct {
	Foo string `param:"foo"`
	Bar string `query:"bar"`
}

func testPostBody(c echo.Context) error {
	var params Params
	if err := c.Bind(&params); err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err)
	}

	return c.String(200, "OK")
}

Version/commit

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