登录
首页 >  Golang >  Go问答

未指定表单的内容类型进行提交

来源:stackoverflow

时间:2024-03-24 16:09:49 212浏览 收藏

在尝试将包含文件输入的表单提交到 Go 服务器时,服务器会抛出错误,表明请求中未设置正确的 Content-Type 标头。尽管表单包含 enctype="multipart/form-data" 属性,但服务器无法识别它作为多部分表单数据,从而导致解析失败。

问题内容

我正在尝试将表单从页面(使用 bootstrap)提交到 go 服务器。所有内容都正确路由,但是当 go 函数尝试解析多部分表单数据时,它会抛出一个错误,声称内容类型不是 multipart/form-data。

这是 html

<form action="/api/submitpost" method="post" enctype="multipart/form-data"> 
    <div class="form-check">
        &lt;input type=&quot;checkbox&quot; name=&quot;rules&quot; class=&quot;form-check-input&quot; id=&quot;rulescheck1&quot; value=&quot;1&quot;&gt;
        <label class="form-check-label" for="rulescheck1">
            i have read the <a href="/rules">rules</a>
        </label>
    </div>
    <div class="form-group">
        &lt;input type=&quot;text&quot; name=&quot;subject&quot; class=&quot;form-control&quot; id=&quot;subjectinput&quot; placeholder=&quot;subject&quot;&gt;
    </div>
    <div class="form-group">
        &lt;input type=&quot;text&quot; name=&quot;body&quot; class=&quot;form-control&quot; id=&quot;bodyinput&quot; placeholder=&quot;body&quot;&gt;
    </div>
    <div class="form-group-sm justify-content-between">
        &lt;input type=&quot;file&quot; name=&quot;file&quot; class=&quot;file&quot; id=&quot;fileinput&quot; data-browse-on-zone-click=&quot;true&quot;&gt;
        &lt;input type=&quot;hidden&quot; name=&quot;boardname&quot; value=&quot;{{ .board }}&quot;&gt;
        <button type="submit" class="btn btn-primary">submit</button>
    </div>
</form>

这是 go 函数(直到返回错误)

func submitpost(r *http.request) error {
    maxsize := int64(32 << 20) // 32mb
    if err := r.parsemultipartform(maxsize); err != nil {
        for name, values := range r.header {
            for _, value := range values {
                println("\t", name, ":", value)
            }
        }
        return errors.new("error parsing, " + err.error())
    }

    ...

}

这是输出(很抱歉奇怪的语法突出显示,我不知道如何修复它)。看来根本没有设置内容类型。

Accept : text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
         Sec-Fetch-User : ?1
         Sec-Fetch-Dest : document
         Referer : http://localhost/tech
         Connection : keep-alive
         Cache-Control : max-age=0
         Upgrade-Insecure-Requests : 1
         User-Agent : [my user agent]
         Sec-Fetch-Site : same-origin
         Sec-Fetch-Mode : navigate
         Accept-Encoding : gzip, deflate, br
         Accept-Language : en-US,en;q=0.9
2020/09/14 11:38:52 err: error parsing, request Content-Type isn't multipart/form-data

解决方案


好吧,发布此内容后不久,我通过在表单操作链接末尾添加“/”解决了该问题。这是我的最终形式。

<form action="/api/submitPost/" method="POST" enctype="multipart/form-data"> 
    <div class="form-check">
        &lt;input type=&quot;checkbox&quot; name=&quot;rules&quot; class=&quot;form-check-input&quot; id=&quot;rulesCheck1&quot; value=&quot;1&quot;&gt;
        <label class="form-check-label" for="rulesCheck1">
            I have read the <a href="/rules">rules</a>
        </label>
    </div>
    <div class="form-group">
        &lt;input type=&quot;text&quot; name=&quot;subject&quot; class=&quot;form-control&quot; id=&quot;subjectInput&quot; placeholder=&quot;Subject&quot;&gt;
    </div>
    <div class="form-group">
        &lt;input type=&quot;text&quot; name=&quot;body&quot; class=&quot;form-control&quot; id=&quot;bodyInput&quot; placeholder=&quot;Body&quot;&gt;
    </div>
    <div class="form-group-sm justify-content-between">
        &lt;input type=&quot;file&quot; name=&quot;file&quot; class=&quot;file&quot; id=&quot;fileInput&quot; data-browse-on-zone-click=&quot;true&quot;&gt;
        &lt;input type=&quot;hidden&quot; name=&quot;boardName&quot; value=&quot;{{ .board }}&quot;&gt;
        <button type="submit" class="btn btn-primary">Submit</button>
    </div>
</form>

本篇关于《未指定表单的内容类型进行提交》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>