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

huangapple go评论71阅读模式
英文:

How to get the status code from within the HTTP request handler

问题

在我的请求处理程序中,我有一个条件语句,我需要获取HTTP状态码。

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

    // 如何获取307状态码,以决定是否重定向?
    if w.StatusCode != 307 { // 不起作用,没有这个字段 - 为什么没有?
        http.Redirect(w, r, idStr, 307)
    } else {
        RenderTemplate()
    }
}

m.HandleFunc("/{id:.*}", PostHandler).Methods("POST") // 这是首先匹配的,用于拦截带有307状态的POST请求
m.HandleFunc("/{id:.*}", MyHandler).Methods("GET", "POST")

我制作了一个示例来帮助说明这个具体的情况:

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

我该如何实现这个功能?

基本上,我使用307是因为我需要重新发送我的POST值到http.Redirect(w, r, url, code)的目标位置。据我所知,这似乎是最好的方法,但是,我没有状态码就无法实现。

附加问题:使用307是一个不好的解决方案吗?如果是,有什么更好的替代方案?

英文:

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?

答案1

得分: 4

你的问题是这样的:

> 如何获取307状态码,以决定是否重定向

你想在一个HTTP请求处理程序中决定是否需要重定向。在请求处理程序中,你不会接收状态码,而是作为响应的一部分发送状态码。

英文:

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.

答案2

得分: 4

我理解你想做什么,但是你的问题很具有误导性。正如许多人所提到的,你无法在重定向后的请求中访问之前发出的响应代码(这不是因为Go不提供,而是HTTP无法按照你期望的方式处理你的情况)。原始请求和重定向请求是应用程序中发生在不同时间的两个独立的HTTP请求。这意味着如果它们完全相同,你无法轻易区分全新的请求和重定向请求。

以下是我想到的解决方案:

  1. 当发送307响应代码时,你应该在Location头部提供一个新的URI。你可以选择不同的URL路径,这样它可以路由到一个单独的处理程序(仅处理重定向请求)。另一个选项是使用相同的路径,但添加一个参数,例如在URL中附加&redirected=1 - 通过解析参数,你可以检测新请求和重定向请求。

  2. 如果可能的话,对于重定向请求使用GET而不是POST。如果POST数据对于第二个请求是必要的,你需要从数据库或其他地方获取原始POST内容。根据我的理解,使用302代码而不是307代码将要求客户端始终使用GET方法进行重定向。然后,你可以使用Request.Method来区分新请求和重定向请求。

  3. 在应用程序中保持状态,并跟踪已经POST过的内容。这将需要每个请求的唯一标识符 - 如果你有并存储它,那么你可以检查请求是全新的还是之前执行过的(假设是重定向)。

英文:

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).

huangapple
  • 本文由 发表于 2015年7月23日 21:04:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/31588195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定