go - 转到r.PostForm为空

标签 go gorilla mux

我试图将表单数据和文件发送到服务器,但是从服务器发送的表单值为空,但是我仅接收文件
服务器

func CreatePost(w http.ResponseWriter, r *http.Request) {
    createdAt := time.Now().String()
    r.ParseForm()
    data := r.PostForm.Get("body")
    fmt.Println(r.PostForm)
    r.ParseMultipartForm(10 << 20)
    file, header, err := r.FormFile("file")
    ///Some Codes///////
}
终端输出
19:33:08 app         | listerning on 8080
19:33:11 app         | map[]

最佳答案

根据ParseForm的文档:

对于其他HTTP方法,或者当Content-Type不是application / x-www-form-urlencoded时,不会读取请求正文,并且r.PostForm初始化为非null的空值。

提交文件时,内容类型将是多部分类型之一。
根据这些文档,我将尝试这样做:

 r.ParseMultipartForm(10<<20)
 data := r.PostForm.Get("body")
 fmt.Println(r.PostForm)
 file, header, err := r.FormFile("file")

关于go - 转到r.PostForm为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62997538/

相关文章:

unit-testing - 优步 Cadence : How do I assert the call to workflow. sleep ()?

go - 如何在中间件中获取路由参数?

go - 即使使用 SkipClean 后也不接受双斜线

c++ - 使用 gstreamer 将 jpeg 混合到 mkv

go - golang中指针内容的差异

Golang 接口(interface)方法链接

google-app-engine - Go 上服务器的 CORS header 问题

go > 如何从 main 重构 http 处理程序

go - 如何实现子路由

go - 在分块数据的 HTTP 响应中如何设置 Content-Length