I am receiving the following POST request json data from react frontend
{
"field_one": "first",
"field_two": "second",
"field_three": "3.00"
}
but i want golang to convert it to this before processing the request
{
"field_one": "first",
"field_two": "second",
"field_three": 3.00
}
I want to convert the field_three
from string to float64, but i am unable to have golang accept the string and process the proper data type
here is my golang function processing the POST request data
func PostCreate(c *fiber.Ctx) error {
type PostCreateData struct {
fieldOne string `json:"field_one" form:"field_one" validate:"required"`
fieldTwo string `json:"field_two" form:"field_two" validate:"required"`
fieldThree float64 `json:"field_three" form:"field_three" validate:"required"`
}
data := PostCreateCreateData{}
if err := c.BodyParser(&data); err != nil {
return err
}
validate := validator.New()
if err := validate.Struct(data); err != nil {
return err
}
postCreate := models.PostCreate{
fieldOne: data.fieldOne,
fieldTwo: data.fieldTwo,
fieldThree: float64(data.fieldThree),
}
database.DB.Create(&postCreate)
return c.JSON(postCreate)
}
Currently the request is not getting processed because the wrong data type for field_three
which is supposed to be float64 but frontend is sending everything as string
What steps am i missing here?