gourav
gourav

Reputation: 177

How to get the form data by postman in short way using golang?

I am retrieving form data using postman, but the code is too long. Is there is any method for getting the data in short form? Here's the code I am using:

Customer struct:

type Customer struct {
 FirstName        string `json:"first_name" bson:"first_name"`
 LastName         string `json:"last_name" bson:"last_name"`
 Email            string `json:"email" bson:"email"`
}  
type Customers []Customer

type new_user struct {
 first_name     string 
 last_name      string 
 email          string
}

Function for retrieving the form data called by the route:

function GetData(c *gin.Context){
 first_name := c.PostForm("first_name")
 last_name := c.PostForm("last_name")
 email := c.PostForm("email")
 reqBody := new(new_user)
 err := c.Bind(reqBody)
 if err != nil {
    fmt.Println(err)
 }
 customer.FirstName = first_name
 customer.LastName = last_name
 customer.Email = email
}

I'm getting 3 form values. Suppose I need to get 50 values, then the function will be much bigger.

Upvotes: 2

Views: 3361

Answers (2)

gourav
gourav

Reputation: 177

As the mkopriva tells me a short way to do this. I got the answer for it. It can be shorter by doing the following code.

type Customer struct {
 FirstName string `form:"first_name" json:"first_name" bson:"first_name"`
 LastName  string `form:"last_name" json:"last_name" bson:"last_name"`
 Email     string `form:"email" json:"email" bson:"email"`
}

In the function the code is:-

customer := new(Customer)
if err := c.Bind(customer); err != nil {
    return nil, err
}
fmt.Println(customer)

It will print the data from the form-data of the postman.

Upvotes: 1

Adrian
Adrian

Reputation: 2113

You can parse HTTP request body yourself, like to following

option 1:

import (
    "github.com/gin-gonic/gin"
    "github.com/gin-gonic/gin/json"
    "log"
)
type Customer struct {
    FirstName        string `json:"first_name" bson:"first_name"`
    LastName         string `json:"last_name" bson:"last_name"`
    Email            string `json:"email" bson:"email"`
}

func process(context *gin.Context) {
    var customer = &Customer{}
    req := context.Request
    err := json.NewDecoder(req.Body).Decode(customer)
    if err != nil {
        log.Fatal()
    }
}

option 2:

Encoding to map to decode to struct (not recommended)

import (
    "github.com/gin-gonic/gin"
    "encoding/json"
    "bytes"
    "log"
)


type Customer struct {
    FirstName        string `json:"first_name" bson:"first_name"`
    LastName         string `json:"last_name" bson:"last_name"`
    Email            string `json:"email" bson:"email"`
}

func Process(context  *gin.Context) {

    req := context.Request
    var aMap = map[string]interface{}{}
    for key, values := range req.PostForm {
        aMap[key]=values[0]
    }

    var buf = new(bytes.Buffer)
    err := json.NewEncoder(buf).Encode(aMap)
    if err != nil {
        log.Fatal(err)
    }
    var customer = &Customer{}
    json.NewDecoder(buf).Decode(customer)
    if err != nil {
        log.Fatal(err)
    }
}

Upvotes: 1

Related Questions