Golang : Gin framework accept query string by post request example
Golang's standard net/http
package has excellent and sufficient functions to handle web applications such as a micro service or API service workload. However, there are times when you want to use third party framework such as Gin instead. For this tutorial, we will explore how to configure a simple Gin web application to accept query string input by POST request such as :
http://example.com/query?firstparameter=somevalue&secondparameter=somevalue
Here we go!
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/query", func(c *gin.Context) {
first := c.Request.URL.Query().Get("first") //<---- here!
c.String(http.StatusOK, "First is "+first+"\n")
second := c.Request.URL.Query().Get("second")
c.String(http.StatusOK, "Second is "+second+"\n")
})
r.Run(":8080") // listen and serve on 0.0.0.0:8080
}
Assuming that you are running this program on localhost... point your browser to
http://localhost:8080/query?first=123&second=abc
and if everything goes smoothly, you should see these reply from the web application.
First is 123
Second is abc
References :
See also : Golang : Get query string value on a POST request
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+42.8k Golang : Get hardware information such as disk, memory and CPU usage
+5.3k PHP : Fix Call to undefined function curl_init() error
+7.5k Golang : get the current working directory of a running program
+14.2k How to automatically restart your crashed Golang server
+4.1k Golang : Converting individual Jawi alphabet to Rumi(Romanized) alphabet example
+6k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+6k Golang : How to get capacity of a slice or array?
+9.4k Golang : Detect number of active displays and the display's resolution
+25k Golang : Convert long hexadecimal with strconv.ParseUint example
+6.6k Golang : Muxing with Martini example
+7.2k Golang : Convert source code to assembly language
+13.2k Golang : Qt progress dialog example