Search code examples
gogo-gin

Does gin-gonic support get request?


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"}


Solution

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

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