#Change URL from server

3 messages · Page 1 of 1 (latest)

gritty hazel
#

i need to send a page with error, s.Error does it, but URL in the browser becomes /api/issue_token instead of whatever URL was before.

// IssueToken is called on POST /api/issue_token by <form> tag
func (s *Server) IssueToken(w http.ResponseWriter, r *http.Request) {
    err := r.ParseForm()
    if err != nil {
        s.Error(w, r, err)
        return
    }

    l, err := s.registrar.Login(r.Context(), &registrar.LoginUserRequest{
        Username: r.Form.Get("username"),
        Password: r.Form.Get("password"),
    })
    if err != nil {
        s.Error(w, r, err)
        return
    }

    http.SetCookie(w, &http.Cookie{
        Name:  "jwt",
        Value: l.Token,
        Path:  "/",
    })

    http.Redirect(w, r, "/feed", http.StatusFound)
}

html which performs the request

<form action="/api/issue_token" method="POST">...

i need to change my handler or <form> somehow, i am not sure how. i could redirect use to a GET /error for example, but i don't want it

buoyant fjord
#

What's happening right now is that <form action creates a POST request when the user submits, sending them to the page specified in action.

You have a few choices:

  1. Send them back using a redirect, like you mentioned
  2. Don't send them to /api/issue_token in the first place. In this case, you might want to have a onsubmit instead of action and use JavaScript to make a fetch to your API instead
#

Not technically a Go question but ^