PostForm getters panic when Context has no request
Author: SoundcreatesCreated Jul 29, 2026Updated Aug 6, 2026
Description
Context.GetPostForm, Context.PostForm, and Context.DefaultPostForm panic when c.Request is nil. This differs from the equivalent query getters, which return empty values safely.
Reproduction
c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.PostForm("key") // panic: nil pointer dereference
Expected behavior
The form getters should behave like the query getters for an empty context:
GetPostForm("key")returns("", false)PostForm("key")returns""DefaultPostForm("key", "fallback")returns"fallback"
Actual behavior
initFormCache calls c.Request.ParseMultipartForm(...) without checking whether c.Request is nil, causing a nil-pointer panic.
Proposed fix
Initialize an empty form cache and return when there is no request. This preserves normal request behavior and matches initQueryCache.
Validation
A regression test covering all three getters passes, along with go test ./... and go vet ./....
Environment
- Gin: master at
34dac20 - Go: 1.26.4
- OS: macOS
Source: gin-gonic/gin