After sending the formdata to the API defined by the http GET method in the golang gin package, can the server receive the data sent through ShouldBind? The intent of that api serves to verify that the requested formdata is valid data.
-
2`ShouldBind` will use the `formBinding` for a `GET` request, and `formBinding` parses query in the URL. So `ShouldBind` works in your case. But please note that a `GET` request don't have a body, and the data is sent as query in the URL. – Zeke Lu Jul 12 '23 at 08:30
1 Answers
In the Golang Gin package, the ShouldBind function is typically used to bind the request data to a struct or a map based on the request's Content-Type. However, the ShouldBind function is commonly used with HTTP POST or PUT requests where the data is sent in the request body, rather than with HTTP GET requests where the data is typically sent as query parameters. In the case of an HTTP GET request, the data is usually sent as query parameters in the URL itself. When using the Golang Gin package, you can access these query parameters using the gin. Context object's Query method. Example:
func YourHandler(c *gin.Context) {
name := c.Query("name")
age := c.Query("age")
// Here you should perform validation on the received data
c.JSON(http.StatusOK, gin.H{
"message": "Data is valid",
})
}
In this example, the name and age query parameters can be accessed using the Query method of the gin—context object. You can then validate these values to ensure they meet your requirements. Query parameters are visible in the URL and may be logged by servers or stored in browser history, so be cautious about including sensitive information in query parameters. If you need to send more complex data or larger payloads, using the HTTP POST method is recommended.

- 569
- 1
- 9
- 31

- 33
- 1
- 8
-
In gin, is it intended that the data delivered as form data inside the handler defined as http get does not work as ShoulderBind? – myskbj Jul 13 '23 at 00:27
-
No, that's not the intended use of form data in an HTTP GET request with the Gin framework. Typically, form data is used with HTTP POST requests to send data to the server. In an HTTP GET request, the parameters are usually sent as part of the URL query string. The Gin framework provides a way to retrieve these query parameters using the gin.Context object. – Hojamuhammet Jul 14 '23 at 11:33