首页 > 技术文章 > go http 请求参数获取

libing029 2020-03-12 23:46 原文

//获取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)
    }
}

 

推荐阅读