douying9296 2019-08-22 14:33
浏览 86
已采纳

路由传入请求

I am trying to create a simple API using Go that performs certain operations depending on the data provided. I was planning to provide JSON data to this API and get details from it for further use.

Since I was trying to provide JSON data I created the routing using gorilla/mux as below:

    router.HandleFunc("/msa/dom/perform-factory-reset?json={jsonData}", CallGet)
    log.Fatal(http.ListenAndServe(":8080", router))

But while trying to hit the endpoint http://localhost:8080/msa/dom/perform-factory-reset?json={"vrf":"ds","ip":"45","mac":"452","method":"gfd"} I am getting 404 page not found error.

Hence I tried to change the implementation such that new routing is done as:

router.HandleFunc("/msa/dom/perform-factory-reset/json={jsonData}", CallGet)

This works absolutely fine and I am able to perform desired tasks. Could someone tell me why this is happening?

  • 写回答

1条回答 默认 最新

  • dongzhan1492 2019-08-22 14:40
    关注

    Is the router gorilla/mux? If so, you cannot add query parameters to path like that. You have to:

      router.Path("/msa/dom/perform-factory-reset").
     Queries("json","{jsonData}").HandlerFunc(CallGet)
    

    If it is some other router, then you still probably have to register path without the query parameters, and then get the query parameter values in the handler from the request.

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

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况