如何在GoFiber中从前端的POST数据中强制执行正确的数据类型?

huangapple go评论85阅读模式
英文:

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!!!

huangapple
  • 本文由 发表于 2022年1月17日 16:02:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/70738155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定