我应该使用什么正则表达式将以下 url 匹配为 full_path?https://edition.cnn.com/search \?q\=test\&size\=10\&category\=us,politics,world,opinion,health(?:www.|http\:\/\/|https\:\/\/).*}不起作用,它只匹配,www.直到搜索 sm := mux.NewRouter() sm.HandleFunc("/", getIndex).Methods("GET") sm.HandleFunc(`/api/v1/hostname/{full_path:(?:www.|http\:\/\/|https\:\/\/).*}`, URLHandler)更新处理程序是:func URLHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) url := vars["full_path"] fmt.Fprintf(w, "Full path is: ", url)}编辑2:这对我有用sm := mux.NewRouter().SkipClean(true).UseEncodedPath()sm.HandleFunc("/", getIndex).Methods("GET")sm.HandleFunc(`/api/v1/hostname/{full_path:.+`, URLHandler)在我使用的处理程序url.PathUnescape(r.URL.RequestURI())和 xurls上提取 urlfunc URLHandler(w http.ResponseWriter, r *http.Request) { URL, _ := url.PathUnescape(r.URL.RequestURI()) ext := xurls.Strict().FindAllString(URL, -1) fmt.Fprintf(w, "Full path is: ", ext)}
1 回答
墨色风雨
TA贡献1853条经验 获得超6个赞
我认为 gorilla mux 只会使用路径部分而不是 URL 的查询字符串。尝试附加RawQuery.
像这样的东西:
func URLHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
url := vars["full_path"] + "?"
if r.URL.RawQuery != "" {
url += "?" + r.URL.RawQuery
}
fmt.Fprintf(w, "Full url is: ", url)
}
- 1 回答
- 0 关注
- 214 浏览
添加回答
举报
0/150
提交
取消
