dpdfh60088 2018-07-21 21:53
浏览 106
已采纳

http.Request r.FormValue不返回任何内容/地图[]

I have the following Go code:

package main

import (
  "encoding/json"
  "fmt"
  "github.com/gorilla/mux"
  "github.com/gorilla/handlers"
  "log"
  "net/http"
  "io/ioutil"
)

type rLog struct {
  Method string
  URI string
  FormParam string
}

func commonMiddleware(next http.Handler) http.Handler {
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.Header().Add("Content-Type", "application/json")
    formBs, err := ioutil.ReadAll(r.Body)
    if err != nil {
      log.Fatalf("Failed to decode postFormByteSlice: %v", err)
    }
    rl := rLog{Method: r.Method, URI: r.RequestURI, FormParam: string(formBs)}
    log.Printf("%+v", rl)
    next.ServeHTTP(w, r)
  })
}

func main() {
  port := ":3000"
  var router = mux.NewRouter()
  router.Use(commonMiddleware)
  router.HandleFunc("/m/{msg}", handleMessage).Methods("GET")
  router.HandleFunc("/n/", handleNumber).Methods("POST")

  headersOk := handlers.AllowedHeaders([]string{"Authorization"})
  originsOk := handlers.AllowedOrigins([]string{"*"})
  methodsOk := handlers.AllowedMethods([]string{"GET", "POST", "OPTIONS"})

  fmt.Printf("Server is running at http://localhost%s
", port)
  log.Fatal(http.ListenAndServe(port, handlers.CORS(originsOk, headersOk, methodsOk)(router)))
}

func handleMessage(w http.ResponseWriter, r *http.Request) {
  vars := mux.Vars(r)
  message := vars["msg"]
  response := map[string]string{"message": message}
  json.NewEncoder(w).Encode(response)
}

func handleNumber(w http.ResponseWriter, r *http.Request) {
  log.Println(r.FormValue("name")) // this returns nothing
  response := map[string]string{"name": "1"} // dummy response
  json.NewEncoder(w).Encode(response)
}

What I try to do here is very simple.

  1. I want to log all POST form data (it's done inside the commonMiddleware)
  2. Access the form data inside the handleNumber i.e. name (and many more later) and do some logic with it.

The problem that I have right now is, the log.Println(r.FormValue("name")) returns nothing and I really wonder why that happened.

enter image description here

I've tried adding r.ParseForm() before log.Println(r.FormValue("name")). But it didn't work.

And, when I add log.Println(r.Form) line, it returns map[].

What did I missed here?

  • 写回答

1条回答 默认 最新

  • doq13207 2018-07-21 23:22
    关注

    You truing to read r.Body twice, first in commonMiddleware with ioutil.ReadAll(r.Body) and then in handleNumber with r.ParseForm(). You can't. It's io.Reader, you can't read it two times. You can instead, for example, r.ParseForm() in middlewear and then use parsed form data. In middlewear r.PosrForm.Encode() to log, and in handler r.FormValue() or r.Form.Get() to extract. I think something like this should do

    func commonMiddleware(next http.Handler) http.Handler {
      return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Header().Add("Content-Type", "application/json")
        err:=r.ParseForm() //parse in middleware, data will be contained in r.PostForm
        if err != nil {
          log.Fatalf("Failed to decode postFormByteSlice: %v", err)
        }
        rl := rLog{Method: r.Method, URI: r.RequestURI, FormParam: r.PostForm.Encode()} //url.Values.Encode() stringifys form data
        log.Printf("%+v", rl)
        next.ServeHTTP(w, r)
      })
    }
    
    func main() {
      port := ":3000"
      var router = mux.NewRouter()
      router.Use(commonMiddleware)
      router.HandleFunc("/m/{msg}", handleMessage).Methods("GET")
      router.HandleFunc("/n/", handleNumber).Methods("POST")
    }
    
    func handleNumber(w http.ResponseWriter, r *http.Request) {
      log.Println(r.PostForm.Get("name")) // or just r.Form.Get("name") or r.FormValue("name")
      response := map[string]string{"name": "1"} // dummy response
      json.NewEncoder(w).Encode(response)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c