net/http:是否可以在http.HandleFunc中使用自定义参数?

huangapple go评论73阅读模式
英文:

net/http : is it possible to have http.HandleFunc custom params?

问题

我正在尝试找到一种方法来创建特殊路由,以便可以使用参数进行POST请求(/url/:param)。

我尝试了以下代码:

http.HandleFunc("/myroute/:myparam", myfunction)

我希望能够在"myfunction"中使用req.Form获取http请求的参数,但它一直失败并返回404错误。

我知道这是一个奇怪的要求,因为我可以将参数传递给函数的主体,但为了方便使用/显示,我被要求能够预先生成一组静态的"路由",以便可以将其分发为不同的URL + 可选参数,而不是为每个人提供一个URL + 参数。

英文:

I'm trying to find a way to create special routes to that would allow post request to work with a params ( /url/:param ).

I've tried something like this :

http.HandleFunc("/myroute/:myparam",myfunction)

And i was hopping to be able to get the params as of the http-request in side "myfunction" with req.Form but it keeps on failing on me and i get 404'ed.

I recognize it is something strange to ask as i could pass my params in the body of the function, but for ease of use/display i've been asked to be able to pregenerate a list of static "routes" on some preset so that it can be distributed as different urls + optional params instead of one url for everybody + params..

答案1

得分: 6

使用默认的Mux是不可能的,但是你可以使用替代的路由器,比如httprouterGorilla Muxchi

下面是使用httprouter的示例代码:

package main

import (
    "fmt"
    "github.com/julienschmidt/httprouter"
    "net/http"
)

func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
    fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}

func main() {
    router := httprouter.New()
    router.GET("/hello/:name", Hello)
    http.ListenAndServe(":8080", router)
}
英文:

It's not possible with the default Mux, but you can use alternative routers like httprouter, Gorilla Mux or chi.

Here is an example of httprouter usage:

package main

import (
    "fmt"
    "github.com/julienschmidt/httprouter"
    "net/http"
)

func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
    fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}

func main() {
    router := httprouter.New()
    router.GET("/hello/:name", Hello)
    http.ListenAndServe(":8080", router)
}

答案2

得分: 0

你可以在/上定义一个处理函数,然后对路径进行自定义解析,比如按/字符进行分割。

http.HandleFunc("/", dispatcher)

这样,你的处理程序将接受所有请求,然后你可以手动将请求路由到相应的函数。例如,PUT调用会在资源后面期望一些参数(/orders/2)。

logs.Info("你正在调用的URL是:" + req.URL.Path)

输出:你正在调用的URL是:/orders/2

你还可以这样做:
http.HandleFunc("/orders/", dispatcher)
来缩小处理程序的范围。这样,你可以将路径的资源部分的验证留给Go的http包处理。

英文:

You can define a handle function on / and then have custom parsing of the path, like splitting on / character.

http.HandleFunc("/", dispatcher)

In this way, your handler will accept everything and you can route the requests manually to the respective function. For example, PUT calls would expect some parameter after the resource (/orders/2)

logs.Info("The URL that you are calling is: " + req.URL.Path)

Output: The URL that you are calling is: /orders/2

You can also do:
http.HandleFunc("/orders/", dispatcher)
to narrow down the scope of your handler. In this way, you can leave the validation of the resource portion of the path to Go http package.

huangapple
  • 本文由 发表于 2016年12月7日 11:38:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/41008999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定