对Golang中的FORM相关字段理解
- 作者: 巴黎的铁塔在思绪8170187
- 来源: 51数据库
- 2021-06-30
form 字段
通过调用request结构体提供的方法,我们可以将url、body、或者以上两者的数据提取到该结构体的form、postform和multipartform等字段中。
(1)调用parseform方法或者parsemultipartform方法,对请求进行分析
(2)访问相应的字段
事例:
package main
import (
"net/http"
"fmt"
)
func process(w http.responsewriter, r *http.request) {
r.parseform()
//parseform 对请求进行语法分析
fmt.fprintln(w,r.multipartform)
}
func main() {
server := http.server{
addr:"127.0.0.1:8080",
}
http.handlefunc("/process",process)
server.listenandserve()
}
创建一个具体表单
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>gowebprograming</title> </head> <body> <form action="http://127.0.0.1:8080/process?hello=world&thread=get" method="post" enctype="application/x-www-form-urlencoded"> <input type="text" name="hello" value="你好 世界"/> <input type="text" name="post" value="456" /> <input type="submit" /> </form> </body> </html>
我们在浏览器运行html文件,结果为:
map[hello:[你好 世界 world] post:[456] thread:[get]]
我们发现这个结构是一个map,他的键为字符串,而建的值是由字符串组成的一个切片。
这个结构总是包含查询的值hello=world, thread=get,还有表单值hello=123和post=456,这些值都进行了url的解码。
比如你好世界之间有空格,说明不是编码之后的%20。
postform 字段
执行语句r.form[“post”]会返回一个切片,切片里包含了表单提交的数据和url中的数据就像“你好世界”和“world” 是一组切片值。但是表单值在切片中总会排在url之前。 ( hello:[你好 世界 world] )
如果我们只想获得表单值而不是url的值,我们可以使用request结构的postform字段,
我们将r.form 改为 r.postform 会出现如下结果
map[hello:[你好 世界] post:[456]]
我们将 enctype="application/x-www-form-urlencoded"改为 enctype=“multipart/form-data”, 结果如下:
map[]
会得到一个空的map,这是为什么呢???
如果我们将 enctype="application/x-www-form-urlencoded"改为 enctype=“multipart/form-data”,并改回 r.form。会出现以下结果:
map[hello:[world] thread:[get]]
这是因为parseform字段只支持"application/x-www-form-urlencoded"编码,所以r.form不会反悔任何表单值,而是只返回url的查询值。
为了解决这个问题,我们需要通过multipartform字段来获取multipart/form-data编码的表单值。
补充:go通过http发送form-data
首先是获取form-data内容
func resendformfile(r *http.request, url string) {
data := r.formvalue("data")
formfile, fileheader, err := r.formfile("pic")
if err != nil {
return
}
_, status := requestpost(formfile, fileheader.filename, []byte(data), url)
if (status / 100) != 2 {
fmt.println("转发图片失败")
}
return
}
然后是发送
func requestpost(formfile multipart.file, filename string, data []byte, posturl string) (resp interface{}, status int) {
buf := new(bytes.buffer)
w := multipart.newwriter(buf)
if fw, err := w.createformfield("data"); err == nil {
fw.write(data)
}
if createformfile, err := w.createformfile("pic", filename); err == nil {
readall, _ := ioutil.readall(formfile)
createformfile.write(readall)
}
w.close()
req, err := http.newrequest(http.methodpost, posturl, buf)
if err != nil {
return
}
// don't forget to set the content type, this will contain the boundary.
req.header.set("content-type", w.formdatacontenttype())
client := &http.client{}
res, err := client.do(req)
if err != nil {
return
}
return res.body, res.statuscode
}
这样返回的body是不可以直接json序列化的
可以先使用ioutil读出来或者byte.buffer进行中转都是比较简单的选择
func unmarshalwriter(body io.readcloser, w http.responsewriter) {
all, _ := ioutil.readall(body)
buffer := bytes.newbuffer(all)
buffer.writeto(w)
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
