dongqingchan2385 2015-07-23 13:04
浏览 67
已采纳

如何从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?

  • 写回答

2条回答 默认 最新

  • douhuocuo9012 2015-07-24 09:57
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向