diff --git a/context.go b/context.go index 1dc730e3..aaba738d 100644 --- a/context.go +++ b/context.go @@ -648,13 +648,18 @@ func (c *Context) PostFormArray(key string) (values []string) { func (c *Context) initFormCache() { if c.formCache == nil { c.formCache = make(url.Values) - req := c.Request - if err := req.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil { - if !errors.Is(err, http.ErrNotMultipart) { - debugPrint("error on parse multipart form array: %v", err) + if c.Request != nil { + var maxMemory int64 = defaultMultipartMemory + if c.engine != nil { + maxMemory = c.engine.MaxMultipartMemory } + if err := c.Request.ParseMultipartForm(maxMemory); err != nil { + if !errors.Is(err, http.ErrNotMultipart) { + debugPrint("error on parse multipart form array: %v", err) + } + } + c.formCache = c.Request.PostForm } - c.formCache = req.PostForm } } diff --git a/context_test.go b/context_test.go index e8d305e4..a8ed18b5 100644 --- a/context_test.go +++ b/context_test.go @@ -3955,3 +3955,24 @@ func BenchmarkGetMapFromFormData(b *testing.B) { }) } } + +func TestContextPostFormWithoutRequest(t *testing.T) { + c, _ := CreateTestContext(httptest.NewRecorder()) + c.Request = nil + + val, ok := c.GetPostForm("key") + assert.False(t, ok) + assert.Empty(t, val) + + val = c.PostForm("key") + assert.Empty(t, val) + + val = c.DefaultPostForm("key", "default_val") + assert.Equal(t, "default_val", val) + + vals := c.PostFormArray("key") + assert.Empty(t, vals) + + mapVals := c.PostFormMap("key") + assert.Empty(t, mapVals) +}