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 求.net core 几款免费的pdf编辑器
  • ¥20 SQL server表计算问题
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)