go - Golang: redirect twice and cause http: multiple response.WriteHeader calls -
i have weird output ... let me post code first explain:
under main function declared
managemux.handlefunc("/info", info)
first log in , redirect "/login" page "/":
func login(w http.responsewriter, r *http.request) { if r.method == "get" { t, err := template.parsefiles("manage/login.html") checkerror(err) t.execute(w, nil) } else { //post r.parseform() //do authentications here http.redirect(w, r, "/", http.statusfound) } }
then redirect page "/info" current page "/" (which has buttons):
func manage(w http.responsewriter, r *http.request) { t, err := template.parsefiles("manage/manage.html") checkerror(err) t.execute(w, nil) r.parseform() if r.form["button"] != nil { //to post actions buttons if r.form["button"][0] == "log" { http.redirect(w, r, "/info", http.statusfound) } } }
at last, made template , show on client side:
const tpl=`stuff inside` type infodefault struct { //stuff inside } func info(w http.responsewriter, r *http.request) { info := infodefault{ //stuff inside } t, err := template.new("info").parse(tpl) checkerror(err) err = t.execute(os.stdout, info) checkerror(err) }
now, weird thing is, when click button on page "/", got error "http: multiple response.writeheader calls". @ same time link called "found" shows on bottom of page (weird!), , when click link "found", got parsed template printed on server side instead of webpage.
does know why...? , how fix error , print stuff on client webpage? thank you!!!
as jimb pointed out: server gets confused because there different status codes associated both writing http.responsewriter
, redirect
. can't both @ same time.
i expand more on how can carry data on next page (assuming redirecting).
headers can write information request object , receive on destination page. example:
func myhandler(w http.responsewriter, r *http.request) { w.header().set("my-awesome-header", "rocks") ... }
session: talking access control far can see, , think persisting user data better done through session. example: can use database or session handler https://github.com/gorilla/sessions. check out thread: best practice sessions (gorilla/sessions).
cookies: i'm not sure kind of front-end using, storing non-sensitive data on cookie option? nothing beats 1 (it has real choc-chip cookies in examples ;-) ): https://astaxie.gitbooks.io/build-web-application-with-golang/content/en/06.1.html.
Comments
Post a Comment