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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度