go http 请求参数获取

//获取url parames
request.ParseForm()
values := request.Form["firs"][0]

//获取头部参数
request.Header.Get("name")


//解决post body 第二次读为空问题

func handleIterceptor(h http.HandlerFunc) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        fmt.Println("handleIterceptor")
        buff, _ := ioutil.ReadAll(r.Body)
        r.Body.Close()//必须关闭后在给body 赋值
        r.Body = ioutil.NopCloser(bytes.NewBuffer(buff))
        fmt.Println(string(buff))
        fmt.Printf("interceptor header %s\n",r.Header.Get("name"))
        h(w, r)
    }
}

猜你喜欢

转载自www.cnblogs.com/libing029/p/12483760.html