2 回答

TA貢獻1951條經驗 獲得超3個贊
答案在ParseForm's source innet/http/request.go
if r.Method == "POST" || r.Method == "PUT" || r.Method == "PATCH" {
r.PostForm, err = parsePostForm(r)
}
如果方法是 POST、PUT 或 PATCH,它只會解析 POST 正文。
r.Method = "POST"您可以通過在調用之前添加來強制執行此操作r.ParseForm:
r.Method = "POST"
r.ParseForm()

TA貢獻1777條經驗 獲得超10個贊
https://golang.org/pkg/net/http/#Request
表單包含解析后的表單數據,包括 URL 字段的查詢參數和 PATCH、POST 或 PUT 表單數據。該字段僅在調用 ParseForm 后可用。HTTP 客戶端忽略 Form 而使用 Body。
如果我們查看代碼,ParseForm我們會看到:
if r.PostForm == nil {
if r.Method == "POST" || r.Method == "PUT" || r.Method == "PATCH" {
r.PostForm, err = parsePostForm(r)
}
if r.PostForm == nil {
r.PostForm = make(url.Values)
}
}
最簡單的方法是讓它認為這是一個 POST 請求(然后再將其設置回來,以防您以后需要真正的方法):
method := r.Method
r.PostForm == nil // this line may necessary
r.Method = http.MethodPost
r.ParseForm()
r.Method = method
- 2 回答
- 0 關注
- 145 瀏覽
添加回答
舉報