gpt4 book ai didi

rest - gorilla /多路复用器请求与网址格式不匹配

转载 作者:行者123 更新时间:2023-12-01 20:22:30 27 4
gpt4 key购买 nike

我正在访问一些我以前写的简单Web服务器的旧代码,并且我的请求模式不再起作用。我有一个初始化我的路线的函数,如下所示:

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

最佳答案

README ...要匹配查询,您需要使用

r := mux.NewRouter()
...
r.Queries("key", "value")

匹配PathPrefix
r.PathPrefix("/products/")

我认为您需要将两者结合起来才能实现所需的功能。

关于rest - gorilla /多路复用器请求与网址格式不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60322355/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com