go - How to get the status code from within the HTTP request handler -
in request handler have conditional statement need fetch http status code.
func posthandler(w http.responsewriter, r *http.request) { params := mux.vars(r) idstr := params["id"] // how 307 status code, decide whether redirect or not? if w.statuscode != 307 { // not work, no such field - why not??? http.redirect(w, r, idstr, 307) } else { rendertemplate() } } m.handlefunc("/{id:.*}", posthandler).methods("post") // matched first intercept post requests status 307 m.handlefunc("/{id:.*}", myhandler).methods("get", "post")
i've made example illustrate concrete scenario:
http://play.golang.org/p/yzgtsvo524
how achieve this?
basically i'm using 307 because need resend post values http.redirect(w,r, url, code) destination. afaik seems best way this, again, can't without status code.
additional question: using 307 bad solution? if so, what's better alternative?
i understand you're trying do, question misleading though. mentioned many people, don't have access previously issued response code on request performed consequence of redirection (not because go doesn't provide http doesn't handle scenario way want it). original , redirected requests 2 separate http requests occurring in 2 different times in application. means can't distinguish between brand new , redirected requests if they're exactly same.
the following solutions pop mind:
when sending 307 response code, you're supposed provide new uri in
location
header. can choose different url path, can routed separate handler (handling redirected requests). other option use same path, add parameter, example attaching&redirected=1
url – parsing parameters can detect new vs redirected requests.use instead of post redirected requests if possible. if post data essential second request need take somehow database or wherever stored original post content. using code 302 instead of 307 – understanding – require clients follow redirection using method. can use
request.method
distinguish between new vs redirected requests.keep state within application , track posted. require unique identifier per request – if have , store check whether request new or performed before (assuming redirection).
Comments
Post a Comment