go - Read multiple time a Reader -


i have 2 http handlers use same http.responsewriter , *http.request , read request body this:

func method1 (w http.responsewriter, r *http.request){     var postdata database.user     if err := json.newdecoder(r.body).decode(&postdata); err != nil {       //return error     } }  func method2 (w http.responsewriter, r *http.request){     var postdata database.user     //this read gives (of course) eof error     if err := json.newdecoder(r.body).decode(&postdata); err != nil {       //return error     } } 

because of need keep these 2 methods separated, , both of them need read request body, best way (if it's possible) seek request body (which readcloser, not seeker?).

actually, miku, i've found out best solution using teereader, changing method1 in way

func method1 (w http.responsewriter, r *http.request){     b := bytes.newbuffer(make([]byte, 0))     reader := io.teereader(r.body, b)      var postdata mystruct     if err := json.newdecoder(reader).decode(&postdata); err != nil {         //return error     }      r.body = ioutil.nopcloser(b) } 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -