当前位置: > > > > gorilla/mux 请求与 URL 模式不匹配
来源:stackoverflow
2024-04-28 08:09:38
0浏览
收藏
积累知识,胜过积蓄金银!毕竟在Golang开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《gorilla/mux 请求与 URL 模式不匹配》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~
问题内容
我正在访问我不久前编写的简单网络服务器的一些旧代码,并且我的请求模式不再起作用。我有一个函数可以初始化我的路线,如下所示:
func (a *app) initializeroutes() { a.router.handlefunc("/api/forecast/detailed?city={city}&state={state}&period={period}", a.detailedforecast).methods("get") a.router.handlefunc("/api/forecast/detailed?city={city}&state={state}", a.detailedforecast).methods("get") a.router.handlefunc("/api/forecast/detailed?random", a.randomdetailedforecast).methods("get") a.router.handlefunc("/api/forecast/hourly?city={city}&state={state}&hours={hours}", a.hourlyforecast).methods("get") a.router.handlefunc("/api/forecast/hourly?city={city}&state={state}", a.hourlyforecast).methods("get") a.router.notfoundhandler = http.handlerfunc(a.custom404handler) }
我的自定义 404 处理程序仅返回 {“error”: “not found”} 的 json
从这里开始,我在应用程序初始化函数的末尾初始化这些路由(底部 3 行):
func (a *app) initialize() { var err error conf.configure() sqldatasource := fmt.sprintf( "postgres://%s:%s@%s:%s/%s?sslmode=disable", conf.sqlusername, conf.sqlpassword, conf.sqlhost, conf.sqlport, conf.sqldbname) a.db, err = sql.open("postgres", sqldatasource) if err != nil { log.fatal(err) } a.redis = redis.newclient(&redis.options{ addr: fmt.sprintf("%s:%s", conf.redishost, conf.redisport), password: conf.redispassword, db: conf.redisdb, }) a.router = mux.newrouter() a.logger = handlers.combinedlogginghandler(os.stdout, a.router) a.initializeroutes() }
我的应用程序在以下功能上运行:
func (a *app) run() { port := fmt.sprintf(":%s", os.getenv("server_port")) log.fatal(http.listenandserve(port, a.logger)) }
我的服务器运行在:
func main() { a := app{} a.initialize() a.run() }
当我尝试向据我所知有效的 url 模式发出请求时,我得到以下信息:
>>> import requests >>> r = requests.get("http://localhost:8000/api/forecast/detailed?city=chicago&state=il") >>> r <response [404]> >>> r.json() {'error': 'not found'}
编辑
我进行了更改,将查询移出 url。我的路线现在如下所示:
a.router.handlefunc("/api/forecast/detailed", a.detailedforecast).queries("city", "{city:[a-za-z+]+}", "state", "{state:[a-za-z+]+}", "period", "{period:[a-za-z+]+}").schemes("http").methods("get") a.router.handlefunc("/api/forecast/detailed", a.detailedforecast).queries("city", "{city:[a-za-z+]+}", "state", "{state:[a-za-z+]+}").schemes("http").methods("get") a.router.handlefunc("/api/forecast/detailed/random", a.randomdetailedforecast).schemes("http").methods("get") a.router.handlefunc("/api/forecast/hourly", a.hourlyforecast).queries("city", "{city:[a-za-z+]+}", "state", "{state:[a-za-z+]+}", "hours", "{hours:[0-9]+}").schemes("http").methods("get") a.router.handlefunc("/api/forecast/hourly", a.hourlyforecast).queries("city", "{city:[a-za-z+]+}", "state", "{state:[a-za-z+]+}").schemes("http").methods("get")
当我提出相同的请求时,我现在看到:
get : unsupported protocol scheme ""
我检查了 url 以查看发送的内容:
2020/02/20 18:19:41 Scheme is: 2020/02/20 18:19:41 Host is: 2020/02/20 18:19:41 Path is: /api/forecast/detailed 2020/02/20 18:19:41 Query is: city=Chicago&state=IL
路径和查询看起来正确,但我不明白为什么该方案无效。我的请求仍然是 r = requests.get("http://localhost:8000/api/forecast/detailed?city=chicago&state=il")
解决方案
来自 …要匹配您需要使用的查询
r := mux.newrouter() ... r.queries("key", "value")
匹配 pathprefix
r.PathPrefix("/products/")
我认为你需要将这两者结合起来才能实现你想要的。
好了,本文到此结束,带大家了解了《gorilla/mux 请求与 URL 模式不匹配》,希望本文对你有所帮助!关注公众号,给大家分享更多Golang知识!