Golang – ParseForm;err = mime:在第一个标记后预期斜杠。

huangapple go评论81阅读模式
英文:

Golang - ParseForm ; err = mime: expected slash after first token

问题

我遇到了这个错误:Error = mime: expected slash after first token。以下是一些细节。

目标是创建一个登录表单,可以从POST请求中提取用户名和密码。

我还测试了使用curl进行POST请求和静态HTML表单,结果出现相同的问题:mime: expected slash after first token

下面是Go代码的片段:

log.Printf("\n\n\t[loginH()] - POST method ...\n")
err := r.ParseForm()
if err != nil {
    // 处理错误,通过日志记录然后返回
    DebugLog.Printf("[loginH()] - ERROR: with r.ParseForm. (err=%v)\n", err)
    log.Printf("[loginH()] - ERROR: with r.ParseForm. (err=%v)\n", err)
}
username := r.Form["username"]
passwd := r.Form["passwd"]
log.Printf("[loginH()] - r.Form ... username=%s and passwd=%s\n", username, passwd)

HTML表单如下:

<form method="POST" action="/login">
    <input type="text" name="username" placeholder="Email Address"/>
    <input type="password" name="passwd" placeholder="Password"/>
    <input type="submit" id="loginBtn" name="login" value="Logon"/>
</form>

输出结果为:

2016/12/15 21:36:07 [loginH()] - POST method ...
2016/12/15 21:36:07 [loginH()] - ERROR: with r.ParseForm. (err=mime: expected slash after first token)
2016/12/15 21:36:07 [loginH()] - r.Form ... username= and passwd=

非常感谢您提供的任何指导/信息/启示。r.Form为空。

英文:

Am getting this Error = mime: expected slash after first token
Some details below.

The goal is a login form that the username and password
can be extracted from the POST.

I also tested a curl post and a static html form --> same issue = mime: expected slash after first token

Snippet of the go code:

log.Printf(&quot;\n\n\t[loginH()] - POST method ...\n&quot;)
err := r.ParseForm()
if err != nil {
        // Handle error here via logging and then return
        DebugLog.Printf(&quot;[loginH()] - ERROR: with r.ParseForm. (err=%v)\n&quot;, err)
        log.Printf(&quot;[loginH()] - ERROR: with r.ParseForm. (err=%v)\n&quot;, err)
}
username := r.Form[&quot;username&quot;]
passwd := r.Form[&quot;passwd&quot;]
log.Printf(&quot;[loginH()] - r.Form ... username=%s and passwd=%s\n&quot;,username,passwd)

The html/form is:

&lt;form method=&quot;POST&quot; action=&quot;/login&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;username&quot; placeholder=&quot;Email Address&quot;/&gt;
    &lt;input type=&quot;password&quot; name=&quot;passwd&quot; placeholder=&quot;Password&quot;/&gt;
    &lt;input type=&quot;submit&quot;  id=&quot;loginBtn&quot; name=&quot;login&quot; value=&quot;Logon&quot;/&gt;
&lt;/form&gt;

Output is:

2016/12/15 21:36:07 [loginH()] - POST method ...
2016/12/15 21:36:07 [loginH()] - ERROR: with r.ParseForm. (err=mime: expected slash after first token)
2016/12/15 21:36:07 [loginH()] - r.Form ... username= and passwd=

Thanks in advance for any pointers/information/enlightenment.

r.Form is empty

答案1

得分: 0

这个错误来自于mediatype.go的第85行。你可能是从内部调用ParseMediaType方法时得到了这个错误。从那里看起来,你的静态表单或curl没有正确设置Content-Type或Content-Disposition的某个值。请检查这些头部是否存在问题值。

根据文档:

// ParseMediaType解析媒体类型值和任何可选参数,根据RFC 1521。媒体类型是Content-Type和Content-Disposition头部中的值(RFC 2183)。
// 成功时,ParseMediaType返回转换为小写并去除空格的媒体类型和非nil的映射。
// 返回的映射params从小写属性映射到保留其大小写的属性值。

英文:

This error comes from line 85 of mediatype.go. You're probably getting this error from a call to ParseMediaType method internally. From there it seems like your static form or curl is not setting some value of Content-Type or Content-Disposition correctly. Please check these headers for any problematic values.

From docs:

// ParseMediaType parses a media type value and any optional
// parameters, per RFC 1521.  Media types are the values in
// Content-Type and Content-Disposition headers (RFC 2183).
// On success, ParseMediaType returns the media type converted
// to lowercase and trimmed of white space and a non-nil map.
// The returned map, params, maps from the lowercase
// attribute to the attribute value with its case preserved.

huangapple
  • 本文由 发表于 2016年12月16日 12:49:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/41177449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定