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

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

無法從 go 中的 r.PostFormValue 中提取值?

無法從 go 中的 r.PostFormValue 中提取值?

Go
倚天杖 2022-08-24 18:54:50
我嘗試使用net / http PostFormValue從HTTP POST請求正文(在我的簡單Go HTTP服務器中)中提取值,當我通常查找任何鍵時,我的輸出是一個空字符串,但在我的情況下,嘗試獲取用于HMAC檢查。我使用Postman使用Gorilla / mux路由器將請求發送到我的localhost:8080實例,并設置了標頭。hub.secretContent-Type: application/x-www-form-urlencoded我的處理程序看起來像這樣:func rootPostHandler(w http.ResponseWriter, r *http.Request) {    var expectedMac []byte    body, err := ioutil.ReadAll(r.Body)    if err != nil {        http.Error(w, err.Error(), http.StatusInternalServerError)        return    }    log.Println("r.Body is:", string(body)) // debug: print the request POST body    message := body // debug: set message just for extra clarity    errParse := r.ParseForm()    if errParse != nil {        // handle err    }    secret := []byte(r.PostFormValue("hub.secret"))    log.Println("secret is: ", string(secret))            mac := hmac.New(sha256.New, secret)    mac.Write(message)    expectedMac = mac.Sum(nil)    fmt.Println("Is HMAC equal? ", hmac.Equal(message, expectedMac))    w.Header().Add("X-Hub-Signature", "sha256="+string(message))}正文:hub.callback=http%253A%252F%252Fweb-sub-client%253A8080%252FbRxvcmOcNk&hub.mode=subscribe&hub.secret=xTgSGLOtPNrBLLgYcKnL&hub.topic=%252Fa%252Ftopic打印等的輸出是空字符串,這意味著它找不到,對吧?我在這里錯過了什么?secrethub.secret
查看完整描述

1 回答

?
森林海

TA貢獻2011條經驗 獲得超2個贊

應用程序將請求正文讀取到以下行上的 EOF:

body, err := ioutil.ReadAll(r.Body)

ParseForm 返回一個空窗體,因為正文位于以下行的 EOF:

errParse := r.ParseForm()

從網絡連接讀取請求正文。無法再次讀取請求正文。

刪除對 ioutil 的調用。ReadAll 或使用從 ioutil 返回的數據創建新的正文讀取器。閱讀全文:

r.Body = io.NopCloser(bytes.NewReader(body))


查看完整回答
反對 回復 2022-08-24
  • 1 回答
  • 0 關注
  • 197 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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