亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用golang http包獲取post數據

使用golang http包獲取post數據

Go
神不在的星期二 2022-03-03 15:56:31
我是 golang 新手,熟悉 http 包。我無法獲取使用郵遞員發送的帖子數據。http://localhost:8084/dbTest是我的 URI。我正在傳遞鍵:hub_id值:1使用表單數據。我嘗試了以下方法,req.ParseForm()fmt.Println("hub_id", req.Form["hub_id"])req.Form.Get("hub_id")但這些方法都不起作用。我得到空洞的回應。以下是我的代碼:package mainimport (    "fmt"    "net/http"    "log") func dbtest(w http.ResponseWriter, req *http.Request) {  req.ParseForm()  fmt.Println("hub_id", req.Form["hub_id"])  req.Form.Get("hub_id")  fmt.Println(req.PostFormValue("hub_id")) //response is empty}func main() {    http.HandleFunc("/dbTest", dbtest)  log.Fatal(http.ListenAndServe(":8084", nil))}當我打印 req 時,我得到以下信息:&{POST /dbTest HTTP/1.1 1 1 map[Origin:[chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop] Connection:[keep-alive] Content-Type:[multipart/form-data; boundary=----WebKitFormBoundarydFOTVjOJMeqOHnS3] Content-Length:[138] Accept-Language:[en-US,en;q=0.8] Cache-Control:[no-cache] Accept-Encoding:[gzip, deflate] Accept:[*/*] User-Agent:[Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36] Postman-Token:[ac7ae3a9-60f6-2146-3f1c-209de7622774]] 0xc210012e70 138 [] false localhost:8084 map[] map[] <nil> map[] 127.0.0.1:34152 /dbTest <nil>}解決方案:我找到了解決方案。因為,內容類型是mulipart/form-data我解析表單的正確方法是使用req.ParseMultipartFormhttp 方法。
查看完整描述

2 回答

?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

您使用的是不正確的Content-Type. 使用時Content-Type:application/x-www-form-urlencoded,r.ParseForm()將正確解析數據。r.Form事后檢查。


查看完整回答
反對 回復 2022-03-03
?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

嘗試


err := r.ParseForm()

v := r.Form

h := v.Get("hub_id")

編輯:我看到你已經嘗試過 ParseForm


查看完整回答
反對 回復 2022-03-03
  • 2 回答
  • 0 關注
  • 409 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號