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
+7.9k Golang : Scan files for certain pattern and rename part of the files
+17.6k Golang : Multi threading or run two processes or more example
+12.7k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+34.1k Golang : Call a function after some delay(time.Sleep and Tick)
+8.3k Golang : Randomize letters from a string example
+7.1k Golang : Calculate BMI and risk category
+18.2k Golang : Defer function inside init()
+13.6k Golang : Read from buffered reader until specific number of bytes
+7.8k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+11.8k Golang : Simple file scaning and remove virus example
+11.6k Golang : Delay or limit HTTP requests example
+5.4k Swift : Convert string array to array example