dongzhitao4839 2015-06-02 20:00
浏览 54
已采纳

在没有第三方路由库的情况下路由PUT请求

In this Youtube video (at around 15:29) of a Golang talk by Blake Mizerany, he talks about how to build a router without using a third party package, covering in detail how to construct a route that has a variable component, such as an id. This is the handler that he uses, with the first line showing how to get the variable component of the route (i.e. the key)

func productHandler(w http.ResponseWriter, r *http.Request){
    key := r.URL.Path[len("/products/":]
    switch r.Method{
    case "GET":
      //do stuff
    case "POST"
      //do stuff
    default:
       http.Error(w, "method not allowed", 405)
    }
}

It's not clear from his presentation though what his actual route looks like.

I'm trying to build a route that handles a put request with an id. When I click an element on my page, it sends a put request to this route

http://localhost:8080/products/1433255183951

I have a route like this

   http.HandleFunc("/products/{id}", doSomethingWithProduct){

   }

and of course have the func

func doSomethingWithProduct(res http.ResponseWriter, req *http.Request{
     key := req.URL.Path[len("/products/"):]

     log.Println(key, "is this logging?? nope")

}

Problem. Even though I have that route set up, and the handler, when I click the element I got a 404 not found, and there's no indication that my function was called (i.e. it's not logging)

Question: How do I create a custom route/func that handles a PUT request to

http://localhost:8080/products/1433255183951
  • 写回答

3条回答 默认 最新

  • douwu7168 2015-06-02 20:07
    关注

    http.HandleFunc does not handle the "capture groups" like you're trying to do with {id}.

    http.HandleFunc("/products/", handler) will match all routes that begin with this pattern. You have to parse the rest of it yourself.

    See ServeMux.

    ServeMux is an HTTP request multiplexer. It matches the URL of each incoming request against a list of registered patterns and calls the handler for the pattern that most closely matches the URL.

    Patterns name fixed, rooted paths, like "/favicon.ico", or rooted subtrees, like "/images/" (note the trailing slash). Longer patterns take precedence over shorter ones, so that if there are handlers registered for both "/images/" and "/images/thumbnails/", the latter handler will be called for paths beginning "/images/thumbnails/" and the former will receive requests for any other paths in the "/images/" subtree.

    Note that since a pattern ending in a slash names a rooted subtree, the pattern "/" matches all paths not matched by other registered patterns, not just the URL with Path == "/".

    Patterns may optionally begin with a host name, restricting matches to URLs on that host only. Host-specific patterns take precedence over general patterns, so that a handler might register for the two patterns "/codesearch" and "codesearch.google.com/" without also taking over requests for "http://www.google.com/".

    ServeMux also takes care of sanitizing the URL request path, redirecting any request containing . or .. elements to an equivalent .- and ..-free URL.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。