Golang Gin retrieve integer data - go

I'm trying to retrieve int data from POST requisitions with Gin, but I'm getting an error saying that the functions (PostForm, or any other) expects strings as arguments. I've tried to search for a function expecting int content, but with no success.
I have a struct do define the content, see the code below.
package userInfo
import(
"net/http"
"github.com/gin-gonic/gin"
)
type Person struct {
Name string
Age int
}
func ReturnPostContent(c *gin.Context){
var user Person
user.Name = c.PostForm("name")
user.Age = c.PostForm("age")
c.JSON(http.StatusOK, gin.H{
"user_name": user.Name,
"user_age": user.Age,
})
}
I was thinking in converting the value to int, but if I have 10 inputs this becomes very difficult and impractible.
The error from user.Age:
cannot use c.PostForm("age") (value of type string) as int value in assignmentcompiler

After a lot of source code reading, I finally found out that all you need is to add a 'form' tag on the required field:
Age int `form:"age"`

user strconv.Atoi(c.PostForm("age"))
complete code:
Person:
type Person struct {
Name string
Age int
}
r.POST("/profile", func(c *gin.Context) {
profile := new(Person)
profile.Name = c.PostForm("name")
profile.Age, _ = strconv.Atoi(c.PostForm("age"))
response := gin.H{
"user_name": profile.Name,
"user_age": profile.Age,
}
c.JSON(http.StatusOK, response)
})

Related

HTTP Request Validation Middleware In Go

I am trying to create a common HTTP request validator middleware function that accepts type (maybe reflect.Type) as an argument and then using the package github.com/go-playground/validator/v10 to be able to unmarshall JSON into struct of mentioned type and validate the struct. I've tried to explain with the following example code...
EXAMPLE
type LoginRequestBody struct {
Username string `json:"username",validate:"required"`
Password string `json:"password",validate:"required"`
}
type SignupReqBody struct {
Username string `json:"username",validate:"required"`
Password string `json:"password",validate:"required"`
Age int `json:"age",validate:"required"`
}
// sample routers with a common middleware validator function
router.POST("/login", ReqValidate("LoginRequestBody"), LoginController)
router.POST("/signup", ReqValidate("SignupReqBody"), SignupController)
func ReqValidate(<something>) gin.HandlerFunc {
return func (c *gin.Context) {
// unmarshalling JSON into a struct
// common validation logic...
c.Next()
}
}
Overall, i wanna achieve the same validator flexibility as there in Node.js using Joi package.
I don't know if it is necessary to use middleware but I was recently trying to do something and I found an excellent tutorial that you can see here.
With Gin You can use binding:
Example:
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
type AnyStruct struct {
Price uint `json:"price" binding:"required,gte=10,lte=1000"`
}
func main() {
engine:=gin.New()
engine.POST("/test", func(context *gin.Context) {
body:=AnyStruct{}
if err:=context.ShouldBindJSON(&body);err!=nil{
context.AbortWithStatusJSON(http.StatusBadRequest,
gin.H{
"error": "VALIDATEERR-1",
"message": "Invalid inputs. Please check your inputs"})
return
}
context.JSON(http.StatusAccepted,&body)
})
engine.Run(":3000")
}
Don't use commas to separate struct tag key-value pairs, use space.
You can use generics (type parameters) to replace <something> but your controllers need to have the concrete type as their argument.
For example:
func ReqValidate[T any](next func(*gin.Context, *T)) gin.HandlerFunc {
return func(c *gin.Context) {
params := new(T)
if err := c.ShouldBindJSON(params); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
next(c, params)
}
}
And then the controllers:
type LoginRequestBody struct {
Username string `json:"username" validate:"required"`
Password string `json:"password" validate:"required"`
}
func LoginController(c *gin.Context, params *LoginRequestBody) {
// ...
}
type SignupReqBody struct {
Username string `json:"username" validate:"required"`
Password string `json:"password" validate:"required"`
Age int `json:"age" validate:"required"`
}
func SignupController(c *gin.Context, params *SignupReqBody) {
// ...
}
And then the routing:
router := gin.Default()
router.POST("/login", ReqValidate(LoginController))
router.POST("/signup", ReqValidate(SignupController))

Is there a way I could declare my preloads in a less error prone way

I have a bunch of preloads to preload nested relations
func Companies(db *database.Database) fiber.Handler {
return func(c *fiber.Ctx) error {
companies := new([]model.Company)
result := db.Preload("Relation.Addresses").
Preload("Relation.Contacts").
Preload("Relation.People").
Preload("Relation.BankAccounts").
Preload(clause.Associations).
Find(&companies)
if result.Error != nil {
return c.JSON(responseKit.RecordNotFoundError())
}
return c.JSON(responseKit.RecordsFoundSuccess(*companies, len(*companies)))
}
}
These are my structs
type Company struct {
PrivateGormModel
Name string `json:"name"`
Relation Relation `gorm:"polymorphic:Owner;" json:"relation"`
}
type Relation struct {
PrivateGormModel
OwnerID uint `json:"ownerID"`
OwnerType string `json:"ownerType"`
Addresses []Address `gorm:"polymorphic:Owner;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"addresses"`
Contacts []Contact `gorm:"polymorphic:Owner;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"contacts"`
People []Person `gorm:"polymorphic:Owner;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"people"`
BankAccounts []BankAccount `gorm:"polymorphic:Owner;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"bankAccounts"`
}
Which I found here and works
https://gorm.io/docs/preload.html
But I think this code is error prone because I pass the field names as a string and the field names might change in the future. Is there a way around this?
You can do this with reflection package. See this part of code:
package main
import (
"fmt"
"reflect"
)
type StructA struct {}
type StructB struct {}
type StructC struct {}
func GetTypeName(data interface{}) string {
if data == nil {
return ""
}
refVal := reflect.TypeOf(data)
if refVal.Kind() != reflect.Struct {
return ""
}
return refVal.Name();
}
func main() {
fmt.Printf("%T\n", StructA{})
fmt.Printf("%#v\n", StructB{})
fmt.Println(GetTypeName(StructC{}))
fmt.Println(GetTypeName(StructB{}))
fmt.Println(GetTypeName(StructA{}))
fmt.Println(GetTypeName("ABC"))
fmt.Println(GetTypeName(nil))
}
It produces the following output:
main.StructA
main.StructB{}
StructC
StructB
StructA
It should help you be compile-time safer, but you need to remember it is a little slower

Parsing JSON using struct

I'm trying to parse JSON using Go. Can anyone tell me why my code is not working as expected?
package main
import (
"encoding/json"
"fmt"
)
type Message struct {
Name string
Body string
Time int64
}
type Person struct {
M Message
}
func get_content() {
body := []byte(`{"person":{"Name":"Alice","Body":"Hello","Time":1294706395881547000}}`)
var data Person
err := json.Unmarshal(body, &data)
if err != nil {
panic(err.Error())
}
fmt.Printf("%v",data.M.Name)
}
func main() {
get_content()
}
I'm expecting it to print the Name.
Go playground Code
There are two problems in the code.
The first one is what #umar-hayat mentioned above -> you are unmarshalling into the data object and you should be aiming at the data.M field.
The second problem is that your JSON's structure doesn't match your struct's structure. Your Person has a single field called M. If we want to represent this as JSON it would look like this:
{
"M": {
"Name": "Joe",
"Body": "Hi",
"time": 2600
}
}
Instead, you have a field called person in your JSON which cannot be matched to any field in your struct. The fact that it's similar to the name of the struct's type doesn't help in any way, I'm afraid.
So, you can either change your JSON and your target:
body := []byte(`{"Name":"Alice","Body":"Hello","Time":1294706395881547000}`)
var data Person
err := json.Unmarshal(body, &data.M)
Or just your JSON:
body := []byte(`{"M":{"Name":"Alice","Body":"Hello","Time":1294706395881547000}}`)
var data Person
err := json.Unmarshal(body, &data)
But it's essential that the names of the fields in your JSON match the names of the fields in your struct. Or, as mentioned by Konstantinos, you can use tags in order to specify particular names with which your struct's fields will be represented in the JSON.
You might find this helpful: https://gobyexample.com/json
Here is how to Unmarshel JSON to the struct. you can check it on Go Playground here:
package main
import (
"encoding/json"
"fmt"
)
type Message struct {
Name string
Body string
Time int64
}
type Person struct {
M Message
}
func get_content() {
body := []byte(`{"Name":"Alice","Body":"Hello","Time":1294706395881547000}`)
var data Person
err := json.Unmarshal(body, &data.M)
if err != nil {
panic(err.Error())
}
fmt.Printf(data.M.Name)
}
func main() {
get_content()
}
Replace data with data.M in below line.
err := json.Unmarshal(body, &data)
As long as you intent to map Json keys on structs whose fields have different names you should add tags:
type Message struct {
Name string `json:"Name"`
Body string `json:"Body"`
Time int64 `json:"Time"`
}
type Person struct {
M Message `json:"person"`
}
You can find more information here
In addition this answer explains in an nutshell the purpose of tags in go.

Decoding JSON object and mapping the values to a struct in Go

I have created a webserver using the library net http. My problem is that I want to create a Struct like below from a JSON object, whose keys names don't match with the struct attributes.
type JsonData struct {
Operation string
IsStaff int
}
The JSON object sent from the server is:
{
"function": "search",
"is_staff": 1
"description": "Test"
}
Most of the solutions I have found are creating another struct, where the JSON keys and struct attribute names match each other. Is there a way to map the JSON decoded to my JsonData struct? Below it is my current function right now.
func handler(w http.ResponseWriter, r *http.Request){
switch r.Method {
case http.MethodPost:
var data JsonData
err := json.NewDecoder(r.Body).Decode(&data)
}
}
Search for "tag" in the json.Marshal documentation.
Basically, you can tag struct fields with special annotations that tell the default un/marshaler to use the given name instead of the (case-insensitive) name of the struct field.
For your struct you can likely just do the following, depending on your actual intent:
type JsonData struct {
Operation string `json:"function"`
IsStaff int `json:"is_staff"`
}
func main() {
jsonstr := `{"function":"search","is_staff":1,"description":"Test"}`
var jd JsonData
err := json.Unmarshal([]byte(jsonstr), &jd)
fmt.Printf("OK: jd=%#v, err=%v\n", jd, err)
// OK: jd=main.JsonData{Operation:"search", IsStaff:1}, err=<nil>
}

Convert URL.Query (map of slices) to struct golang

It would be awesome to have a straight forward mapping from the standard library URL.Query() to an struct.
Query() returns a map like:
map[a:[aaaa] b:[bbbb] c:[cccc]]
The struct looks like:
type Thing struct {
A string
B string
C string
}
I've no idea why URL.Query returns a map with array elements inside tough. (well.. I know why but a GET is not likely to have duplicated params)
Please find below the complete example of parsing get query params directly in a golang struct and then sending the struct back as response
package main
import (
"log"
"net/http"
"encoding/json"
"github.com/gorilla/schema"
)
var decoder = schema.NewDecoder()
type EmployeeStruct struct {
MemberId string `schema:"memberId"`
ActivityType string `schema:"activityType"`
BusinessUnitCode int `schema:"businessUnitCode"`
}
func GetEmployee(w http.ResponseWriter, r *http.Request) {
var employeeStruct EmployeeStruct
err := decoder.Decode(&employeeStruct, r.URL.Query())
if err != nil {
log.Println("Error in GET parameters : ", err)
} else {
log.Println("GET parameters : ", employeeStruct)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(employeeStruct)
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/GetEmployee", GetEmployee)
log.Fatal(http.ListenAndServe(":8080", mux))
}
Steps to execute & Test (Assuming you are saving above code in employee.go) :
Step 1 : go run employee.go
Step 2 : Open in browser http://localhost:8080/GetEmployee?memberId=123&activityType=Call&businessUnitCode=56
Step 3 : You should get below response in browser window
{
"MemberId": "123",
"ActivityType": "Call",
"BusinessUnitCode": 56
}
Step 4 : On console you should see below
GET parameters : {123 Call 56}
example:
filters={"reference":["docker.io/library/alpine:latest"]}
need url encode to:
filters=%7B%22reference%22%3A%5B%22docker.io%2Flibrary%2Falpine%3Alatest%22%5D%7D
and could use "github.com/gorilla/schema"
query := struct {
All bool
Filters map[string][]string `schema:"filters"`
Digests bool
Filter string
}{}
decoder := schema.NewDecoder()
decoder.Decode(&query, r.URL.Query())
As pointed out by #mh-cbon gorilla schema is the ultimate solution here.
Instead for obtaining the queryParams from the URL attribute.
func handleRequest(w http.ResponseWriter, r *http.Request) {
queryString := r.URL.Query()
//...parsing the Values -> map[string][]string
}
The approach of gorilla schema is to ship r.PostForm to the decode function.
func handleRequest(w http.ResponseWriter, r *http.Request) {
err := decoder.Decode(person, r.PostForm)
//...using reflect each struct's property can be called using
// the PostForm(url string, data url.Values) signature
fmt.Print(person.GoodJobGorilla)
}
Just parse the string to URL and after you can use the lib github.com/gorilla/schema to parse it :)
// Example to parse querystring to struct
package main
import (
"log"
"net/url"
"github.com/gorilla/schema"
)
type URLParams struct {
Code string `schema:"code"`
State string `schema:"state"`
}
func main() {
var (
params URLParams
decoder = schema.NewDecoder()
)
p := "https://www.redirect-url.com?code=CODE&state=RANDOM_ID"
u, _ := url.Parse(p)
err := decoder.Decode(&params, u.Query())
if err != nil {
log.Println("Error in Decode parameters : ", err)
} else {
log.Printf("Decoded parameters : %#v\n", params)
}
}
https://go.dev/play/p/CmuPhdKh6Yg
Using ggicci/httpin
Disclaimer: I'm the creator and maintainer of this package.
httpin helps you easily decoding HTTP request data from
Query parameters, e.g. ?name=john&is_member=true
Headers, e.g. Authorization: xxx
Form data, e.g. username=john&password=******
JSON/XML Body, e.g. POST {"name":"john"}
Path variables, e.g. /users/{username}
File uploads
How to use?
type ListUsersInput struct {
Page int `in:"query=page"`
PerPage int `in:"query=per_page"`
IsMember bool `in:"query=is_member"`
}
func ListUsers(rw http.ResponseWriter, r *http.Request) {
input := r.Context().Value(httpin.Input).(*ListUsersInput)
if input.IsMember {
// Do sth.
}
// Do sth.
}
httpin is:
well documented: at https://ggicci.github.io/httpin/
well tested: coverage over 98%
open integrated: with net/http, go-chi/chi, gorilla/mux, gin-gonic/gin, etc.
extensible (advanced feature): by adding your custom directives. Read httpin - custom directives for more details.
awesome mentioned: https://github.com/avelino/awesome-go#forms
You can use the graceful package of Echo.
I write some codes as an example, with self-explanatory comments
package main
import (
"log"
"github.com/labstacks/echo"
)
// Declare your struct with form: "" tag
type Employee struct {
MemberId string `form:"memberId"`
ActivityType string `form:"activityType"`
BusinessUnitCode int `form:"businessUnitCode"`
}
// Your handlers should look like this method
// Which takes an echo.Context and returns an error
func GetEmployee(ctx echo.Context) error{
var employee Employee
// With Bind, you can get the Post Body or query params from http.Request
// that is wrapped by echo.Context here
if err := ctx.Bind(&employee);err != nil {
return err
}
// now you can use your struct , e.g
return ctx.json(200, employee.MemberId)
}
// now use the handler in your main function or anywhere you need
func main() {
e := echo.New()
e.Get("/employee", GetEmployee)
log.Fatal(e.Start(":8080"))
}
If anyone is using Echo, query struct tag will be useful for this case.
Example Struct
type struct Name {
FirstName string `query:"first_name"`
LastName string `query:"last_name"`
}
Example Query Param
?first_name="shahriar"&last_name="sazid"
Code
var name Name
err := c.Bind(&name); if err != nil {
return c.String(http.StatusBadRequest, "bad request")
}

Resources