佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > 對(duì)Golang中的FORM相關(guān)字段理解

對(duì)Golang中的FORM相關(guān)字段理解

熱門標(biāo)簽:服務(wù)外包 呼叫中心市場(chǎng)需求 Linux服務(wù)器 鐵路電話系統(tǒng) 地方門戶網(wǎng)站 AI電銷 網(wǎng)站排名優(yōu)化 百度競(jìng)價(jià)排名

Form 字段

通過(guò)調(diào)用Request結(jié)構(gòu)體提供的方法,我們可以將URL、Body、或者以上兩者的數(shù)據(jù)提取到該結(jié)構(gòu)體的Form、PostForm和MultipartForm等字段中。

(1)調(diào)用ParseForm方法或者ParseMultipartForm方法,對(duì)請(qǐng)求進(jìn)行分析

(2)訪問(wèn)相應(yīng)的字段

事例:

package main
import (
 "net/http"
 "fmt"
)
func process(w http.ResponseWriter, r *http.Request) {
 r.ParseForm()
 //ParseForm 對(duì)請(qǐng)求進(jìn)行語(yǔ)法分析
 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=worldthread=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>

我們?cè)跒g覽器運(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的解碼。

比如你好世界之間有空格,說(shuō)明不是編碼之后的%20。

PostForm 字段

執(zhí)行語(yǔ)句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è)問(wèn)題,我們需要通過(guò)MultipartForm字段來(lái)獲取multipart/form-data編碼的表單值。

補(bǔ)充:go通過(guò)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讀出來(lái)或者byte.Buffer進(jìn)行中轉(zhuǎn)都是比較簡(jiǎ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ò)誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • golang中json小談之字符串轉(zhuǎn)浮點(diǎn)數(shù)的操作
  • go浮點(diǎn)數(shù)轉(zhuǎn)字符串保留小數(shù)點(diǎn)后N位的完美解決方法
  • 解決Golang中g(shù)oroutine執(zhí)行速度的問(wèn)題
  • 解決golang結(jié)構(gòu)體tag編譯錯(cuò)誤的問(wèn)題
  • golang 實(shí)現(xiàn)Location跳轉(zhuǎn)方式
  • 解決golang post文件時(shí)Content-Type出現(xiàn)的問(wèn)題
  • golang 打印error的堆棧信息操作
  • golang 比較浮點(diǎn)數(shù)的大小方式

標(biāo)簽:湖南 仙桃 黃山 蘭州 衡水 湘潭 銅川 崇左

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《對(duì)Golang中的FORM相關(guān)字段理解》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    清远市| 从江县| 马关县| 辽宁省| 连州市| 博乐市| 同仁县| 苍梧县| 尚义县| 阳新县| 银川市| 浦县| 霍邱县| 石景山区| 侯马市| 蒲江县| 英德市| 都匀市| 东山县| 清徐县| 鸡西市| 兴宁市| 绥阳县| 宁蒗| 唐山市| 庆安县| 明水县| 石楼县| 上杭县| 凤城市| 阿合奇县| 通化县| 台安县| 黔西县| 仪陇县| 常熟市| 昌都县| 乌海市| 石台县| 威信县| 阿克苏市|