對Golang中的FORM相關(guān)字段理解
Form 字段
通過調(diào)用Request結(jié)構(gòu)體提供的方法,我們可以將URL、Body、或者以上兩者的數(shù)據(jù)提取到該結(jié)構(gòu)體的Form、PostForm和MultipartForm等字段中。
(1)調(diào)用ParseForm方法或者ParseMultipartForm方法,對請求進(jìn)行分析
(2)訪問相應(yīng)的字段
事例:
package main
import (
"net/http"
"fmt"
)
func process(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
//ParseForm 對請求進(jìn)行語法分析
fmt.Fprintln(w,r.MultipartForm)
}
func main() {
server := http.Server{
Addr:"127.0.0.1:8080",
}
http.HandleFunc("/process",process)
server.ListenAndServe()
}
創(chuàng)建一個(gè)具體表單
<!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>
我們在瀏覽器運(yùn)行html文件,結(jié)果為:
map[hello:[你好 世界 world] post:[456] thread:[get]]
我們發(fā)現(xiàn)這個(gè)結(jié)構(gòu)是一個(gè)map,他的鍵為字符串,而建的值是由字符串組成的一個(gè)切片。
這個(gè)結(jié)構(gòu)總是包含查詢的值hello=world, thread=get,還有表單值hello=123和post=456,這些值都進(jìn)行了url的解碼。
比如你好世界之間有空格,說明不是編碼之后的%20。
PostForm 字段
執(zhí)行語句r.Form[“post”]會(huì)返回一個(gè)切片,切片里包含了表單提交的數(shù)據(jù)和url中的數(shù)據(jù)就像“你好世界”和“world” 是一組切片值。但是表單值在切片中總會(huì)排在url之前。 ( hello:[你好 世界 world] )
如果我們只想獲得表單值而不是url的值,我們可以使用Request結(jié)構(gòu)的PostForm字段,
我們將r.Form 改為 r.PostForm 會(huì)出現(xiàn)如下結(jié)果
map[hello:[你好 世界] post:[456]]
我們將 enctype="application/x-www-form-urlencoded"改為 enctype=“multipart/form-data”, 結(jié)果如下:
map[]
會(huì)得到一個(gè)空的map,這是為什么呢???
如果我們將 enctype="application/x-www-form-urlencoded"改為 enctype=“multipart/form-data”,并改回 r.Form。會(huì)出現(xiàn)以下結(jié)果:
map[hello:[world] thread:[get]]
這是因?yàn)镻arseForm字段只支持"application/x-www-form-urlencoded"編碼,所以r.Form不會(huì)反悔任何表單值,而是只返回url的查詢值。
為了解決這個(gè)問題,我們需要通過MultipartForm字段來獲取multipart/form-data編碼的表單值。
補(bǔ)充:go通過http發(fā)送form-data
首先是獲取form-data內(nèi)容
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("轉(zhuǎn)發(fā)圖片失敗")
}
return
}
然后是發(fā)送
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進(jìn)行中轉(zhuǎn)都是比較簡單的選擇
func UnmarshalWriter(body io.ReadCloser, w http.ResponseWriter) {
all, _ := ioutil.ReadAll(body)
buffer := bytes.NewBuffer(all)
buffer.WriteTo(w)
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Golang使用Decimal庫避免運(yùn)算中精度損失詳細(xì)步驟
decimal是為了解決Golang中浮點(diǎn)數(shù)計(jì)算時(shí)精度丟失問題而生的一個(gè)庫,使用decimal庫我們可以避免在go中使用浮點(diǎn)數(shù)出現(xiàn)精度丟失的問題,下面這篇文章主要給大家介紹了關(guān)于Golang使用Decimal庫避免運(yùn)算中精度損失的相關(guān)資料,需要的朋友可以參考下2023-06-06
Go strconv包實(shí)現(xiàn)字符串和基本數(shù)據(jù)類型轉(zhuǎn)換的實(shí)例詳解
在Go語言(Golang)的編程實(shí)踐中,strconv包是一個(gè)非常重要的標(biāo)準(zhǔn)庫,它提供了在基本數(shù)據(jù)類型(如整型、浮點(diǎn)型、布爾型)和字符串之間的轉(zhuǎn)換功能,本文給大家介紹了關(guān)于Go語言字符串轉(zhuǎn)換strconv,需要的朋友可以參考下2024-09-09
如何解析golang中Context在HTTP服務(wù)中的角色
這篇文章主要介紹了如何解析golang中Context在HTTP服務(wù)中的角色問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Go語言高效I/O并發(fā)處理雙緩沖和Exchanger模式實(shí)例探索
這篇文章主要介紹了Go語言高效I/O并發(fā)處理雙緩沖和Exchanger模式實(shí)例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01

