diff --git a/binding/binding_test.go b/binding/binding_test.go index 183d111f..5b311764 100644 --- a/binding/binding_test.go +++ b/binding/binding_test.go @@ -195,6 +195,13 @@ func TestBindingDefault(t *testing.T) { assert.Equal(t, YAML, Default("PUT", MIMEYAML)) } +func TestBindingJSONNilBody(t *testing.T) { + var obj FooStruct + req, _ := http.NewRequest(http.MethodPost, "/", nil) + err := JSON.Bind(req, &obj) + assert.Error(t, err) +} + func TestBindingJSON(t *testing.T) { testBodyBinding(t, JSON, "json", diff --git a/binding/json.go b/binding/json.go index 310922c1..f968161b 100644 --- a/binding/json.go +++ b/binding/json.go @@ -6,6 +6,7 @@ package binding import ( "bytes" + "fmt" "io" "net/http" @@ -24,6 +25,9 @@ func (jsonBinding) Name() string { } func (jsonBinding) Bind(req *http.Request, obj interface{}) error { + if req == nil || req.Body == nil { + return fmt.Errorf("invalid request") + } return decodeJSON(req.Body, obj) }