1

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?

uberrebu
  • 3,597
  • 9
  • 38
  • 73
  • does [this](https://stackoverflow.com/questions/9452897/how-to-decode-json-with-type-convert-from-string-to-float64) help? – blackgreen Jan 17 '22 at 08:13
  • what will be in `a` and `b`? in `err := json.Unmarshal([]byte(a), b) ` am thinking `b` will be `&data` but what will be in `a` and what line position do i place the line of code? – uberrebu Jan 17 '22 at 08:35

1 Answers1

1

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

uberrebu
  • 3,597
  • 9
  • 38
  • 73