我嘗試使用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))
- 1 回答
- 0 關注
- 197 瀏覽
添加回答
舉報
0/150
提交
取消