douke7274 2019-06-08 22:50
浏览 42
已采纳

gin-gonic支持会收到请求吗?

I am new in gin-gonic framework and i have been trying to read the values from the inputs that i added in a get request from html but i have not been able to read the values that i wrote.

When i submit the request the browser sends this url : http://localhost:3000/backend?name1=value1&name2=value2&name3=value3

I have been looking in the internet where gin-gonic uses this url type but i have only found that it uses url like this one http://localhost:3000/backend/value1

html code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <form method="GET" action="/backend">
        <input id="store" name="name1" type="text">
        <input id="razon" name="name2" type="text">
        <input id="total" name="name3" type="text">
        <input type="submit" value="submit">
    </form>
</body>
</html>

golang code:

package main

import(
"net/http"
"fmt"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
 )

func main(){
router := gin.Default()

router.Use(static.Serve("/",static.LocalFile("./views",true)))

router.GET("/backend",func(c *gin.Context){
    fmt.Println(c.Param("name1"))
    c.JSON(http.StatusOK, gin.H{
        "name1" : c.Param("name1"),
    })
})


router.Run(":3000")
}

Actual result: {"name1":""}

Expected result: {"name1":"value1"}

  • 写回答

1条回答 默认 最新

  • douguan3470 2019-06-08 23:01
    关注

    The function you are looking for is not Param(), it's Query().

    https://github.com/gin-gonic/gin#querystring-parameters

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

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示