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 Stata链式中介效应代码修改
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 添加组件无法加载页面,某块加载卡住
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用