![](/img/trans.png)
[英]how to redirect from http to https if we explicitly give http in the url
[英]How to redirect to URL from webhook
我開發了一個 Go 應用程序。 已集成 webhook。 我已成功從 webhook 獲取有效負載(JSON 格式)。 但是,我似乎無法進行重定向。
第 1 步:用戶在 /live 頁面付款。
第 2 步:支付成功后,我的支付處理公司向我的 webhook (/paymentssucess) 發送一個 POST 請求。 付款 function 被調用。 在 payment() 中,我能夠解碼 JSON 數據,這些數據是 POST 到 /paymentsucess 和 fmt.Println 的。
http.HandleFunc("/paymentsuccess", payment)
現在在第 3 步中,我想將用戶(仍然看到 /live)重定向到成功付款的頁面(/thankyou)。 在發布到 webhook 的 JSON 中,有一個支付狀態字段。 我能夠做一個 if 條件來檢查該字段是否具有“成功”值。 在這種情況下,fmt.Println("redirecting toThank you")。 這一切都很好。 然后在下一行(在 payment() 函數中),我添加了以下內容
http.Redirect(w, r, "/thankyou", http.StatusSeeOther)
我期待當前的客戶端頁面(仍然在 /live)重定向到 /thankyou。 但這並沒有發生。
應該如何處理 webhook 重定向?
更新當 webhook 成功發布到 payment() 時,我收到以下響應和請求
回復
&{0xc00029c1e0 0xc0002a0700 0xc000386300 0x1120e20 false false false false 0 {0 0} 0xc000386200 {0xc0003a67e0 map[] false false} map[] false 0 -1 0 false false [] 0 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] [0 0 0] 0xc0003481c0 0}
要求
&{POST /paymentsuccess HTTP/1.1 1 1 map[Accept:[*/*] Accept-Encoding:[gzip] Content-Length:[3162] Content-Type:[application/json] User-Agent:[Omise/2019-05-29] X-Datadog-Parent-Id:[6096593905335487199] X-Datadog-Sampling-Priority:[1] X-Datadog-Trace-Id:[8016780258334486542] X-Forwarded-For:[52.74.199.175] X-Forwarded-Proto:[https]] 0xc000386300 <nil> 3162 [] false d4e7c3fca000.ngrok.io:443 map[] map[] <nil> map[] [::1]:60999 /paymentsuccess <nil> <nil> <nil> 0xc000386340}
更新 2感謝@gipsy,我能夠實施他建議的步驟。 一直到第 4 步。我有一個 goroutine,每兩秒輪詢一次查詢 mysql 數據庫以檢查付款狀態。 一旦這樣,我就可以相應地 fmt.Println 了。 這樣可以確認我可以確認付款。 但是...我無法重定向當前客戶端頁面(用戶在 /live)...
func startPolling1(ShopName, SourceID, Status string) (string, bool) {
for {
time.Sleep(2 * time.Second)
Amount, Result := queryDBForPaymentSuccess(ShopName, SourceID, Status)
if Result == true {
fmt.Println("found result")
fmt.Println("no more polling")
//HOW DO I DO A REDIRECT HERE? http.Redirect won't work because of error "superfluous response.WriteHeader call"
break
}
}
return Amount, Result
}
func livemode(res http.ResponseWriter, req *http.Request) {
if !alreadyLoggedIn(req) {
http.Redirect(res, req, "/", http.StatusSeeOther)
return
}
Shop = getUser(res, req)
if req.Method == http.MethodPost {
//Some codes here
go startPolling1(Shop.Shopname, Charge.Source.ID, "successful")
//Some codes here
}
}
tpl.ExecuteTemplate(res, "golive.html", data1)
}
首先,到達您的服務器的 web-hook 請求完全獨立於您從用戶那里獲得的請求。 所以你在 web-hook 句柄中嘗試的重定向是行不通的
我認為可以有很多方法來處理這個問題。 一種方法可能是:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.