英文:
Accept image header request
问题
我写了一个路由函数,只接受图像请求。看一下下面的代码。
// 添加图像路由路径
func addImage(path string, handler httpImageFunc, name ...string) {
//log.Println("添加图像")
if len(name) > 0 {
router.Handle(path, httpImageFunc(handler)).
Methods("GET").
Headers("Accept", "image/").
Name(name[0])
} else {
router.Handle(path, httpImageFunc(handler)).
Methods("GET").
Headers("Accept", "image/")
}
}
如你所见,请求只接受带有头部("Accept", "image/*")的请求。
但是Chrome浏览器发送的请求头是:
Accept:image/webp,/;q=0.8
而Firefox发送的请求头是:
Accept:image/png,image/;q=0.8,/*;q=0.5
在这种情况下,当Firefox和Chrome发出图像请求时,它们不会通过路由器。
我应该怎么做,让请求能够通过呢?我正在使用Gorilla Mux作为路由器。
英文:
I wrote a route function, that accept only image request. Look at the following code.
// Add image route paths
func addImage(path string, handler httpImageFunc, name ...string) {
//log.Println("Add image")
if len(name) > 0 {
router.Handle(path, httpImageFunc(handler)).
Methods("GET").
Headers("Accept", "image/*").
Name(name[0])
} else {
router.Handle(path, httpImageFunc(handler)).
Methods("GET").
Headers("Accept", "image/*")
}
}
As you can see, the requests accept only with header ("Accept", "image/*").
But chrome browser send a request with header
> Accept:image/webp,/;q=0.8
and firefox with
> Accept:image/png,image/;q=0.8,/*;q=0.5
In this case, when firefox and chrome make an image request, it does not go through the router.
What do I have to do, that the request would be get through? I am using gorilla mux as router.
答案1
得分: 1
使用MatcherFunc并自己检查标头,这样你就可以匹配"image/"。
英文:
Use MatcherFunc and inspect the header yourself so you can match on "image/".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论