如何从HTTP请求处理程序中获取状态代码

In my request handler I have a conditional statement where I need to fetch the http status code.

func PostHandler(w http.ResponseWriter, r *http.Request) {
    params := mux.Vars(r)
    idStr := params["id"]

    // how would I get the 307 status code, to decide whether to redirect or not?
    if w.StatusCode != 307 { // does not work, no such field - why not???
        http.Redirect(w, r, idStr, 307)
    } else {
        RenderTemplate()
    }
}

m.HandleFunc("/{id:.*}", PostHandler).Methods("POST") // this is matched first to intercept POST requests with status 307
m.HandleFunc("/{id:.*}", MyHandler).Methods("GET", "POST")

I've made an example to help illustrate this concrete scenario:

http://play.golang.org/p/YZgTsVO524

How would I achieve this?

Basically I'm using the 307 because I need to resend my POST values the http.Redirect(w,r, url, code) destination. Afaik this seems to be the best way to do this, but again, I can't do it without the status code.

Additional question: is using 307 a bad solution? If so, what's a better alternative?

I understand what you're trying to do, your question is very misleading though. As mentioned by many people, you don't have access to previously issued response code on request performed as a consequence of redirection (not because Go doesn't provide it but HTTP doesn't handle your scenario the way you want it). The original and redirected requests are two separate HTTP requests occurring in two different times in your application. It means you can't easily distinguish between brand new and redirected requests if they're exactly the same.

The following solutions pop into my mind:

  1. When sending 307 response code, you're supposed to provide a new URI in the Location header. You can choose different URL path, so it can be routed to a separate handler (handling only redirected requests). Other option would be to use the same path, but add a parameter, for example attaching &redirected=1 to the URL – by parsing parameters you can then detect new vs redirected requests.

  2. Use GET instead of POST for redirected requests if possible. If POST data is essential for the second request you would need to take it somehow from the database or wherever you stored the original POST content. Using code 302 instead of 307 would – to my understanding – require clients to always follow redirection using GET method. You can then use Request.Method to distinguish between new vs redirected requests.

  3. Keep state within your application and track what was already POSTed. This would require some unique identifier per request – if you have it and store it you could then check whether request is completely new or was performed before (assuming redirection).

Your problem is this:

how would I get the 307 status code, to decide whether to redirect or not

You are trying to decide if you need to redirect or not in a HTTP Request Handler. In a request handler you do not receive the status code, you send the status code as part of the response.