dongzhi4073 2019-05-20 20:32
浏览 46
已采纳

为什么我在GET之后放置DELETE路由器,但未触发DELETE端点

I was creating restful API with Golang and I put the delete router after the getOne router by index, and the delete router never got triggered? I don't know why? When I reverse them, it works! Can someone know the reason??

This is for Building Restful API with Golang.

Not works:

myRouter.HandleFunc("/article", createNewArticle).Methods("POST")

myRouter.HandleFunc("/article/{id}", getOneArticle)

myRouter.HandleFunc("/article/{id}", deleteArticle).Methods("DELETE")

Works!

myRouter.HandleFunc("/article", createNewArticle).Methods("POST")
myRouter.HandleFunc("/article/{id}", deleteArticle).Methods("DELETE")
myRouter.HandleFunc("/article/{id}", getOneArticle)

When I test the api using postman with DELETE methods, it always trigger the getOneArticle and respond with the delete item, but did not actually delete in the database!

  • 写回答

1条回答 默认 最新

  • dsj0312 2019-05-21 00:36
    关注

    You're using gorilla/mux. When an HTTP request comes in, this router tries to match routes in the order in which you add them.

    When you add the route without specifying an HTTP method, it applies to all HTTP methods.

    So, if your more specific route matching the DELETE method comes first, then it will match DELETE method calls, and the next route will match all methods. While it would match DELETE also, an actual DELETE call would never reach it because of the route preceding it.

    But if you reverse them, the route which doesn't specify an HTTP method will match all methods, including DELETE.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端