英文:
how do i enforce the proper data types on gofiber golang from POST data from frontend?
问题
我正在从React前端接收到以下POST请求的JSON数据:
{
"field_one": "first",
"field_two": "second",
"field_three": "3.00"
}
但是在处理请求之前,我希望使用Golang将其转换为以下格式:
{
"field_one": "first",
"field_two": "second",
"field_three": 3.00
}
我想将field_three
从字符串转换为float64,但是我无法让Golang接受字符串并处理正确的数据类型。
这是我处理POST请求数据的Golang函数:
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 := PostCreateData{}
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)
}
目前请求无法处理,因为field_three
的数据类型错误,它应该是float64,但前端将所有内容都发送为字符串。
我在这里漏掉了哪些步骤?
英文:
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?
答案1
得分: 1
通过更新函数来修复问题:
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 string `json:"field_three" form:"field_three" validate:"required"`
}
data := PostCreateData{}
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: data.fieldThree,
}
database.DB.Create(&postCreate)
return c.JSON(postCreate)
}
所以将结构体更新为在请求体中接收字符串:
fieldThree string `json:"field_three" form:"field_three" validate:"required"`
现在一切都正常工作了!
英文:
fixed by updating the function to this
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,string" 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)
}
so updated the struct to expect string in the body
fieldThree float64 `json:"field_three,string" form:"field_three" validate:"required"`
and works like a charm!!!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论