英文:
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("\n\n\t[loginH()] - POST method ...\n")
err := r.ParseForm()
if err != nil {
// Handle error here via logging and then return
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)
The html/form is:
<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>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论