我有以下代码适用JSON
于 POST 请求的正文,但我现在想将其转换form-data
为在请求正文中使用
这是我所拥有的
func Signin(c *fiber.Ctx) error {
var data map[string]string
if err := c.BodyParser(&data); err != nil {
return err
}
var user models.User
findUser := database.DB.Where("email = ?", data.email).First(&user)
token, err := middlewares.GenerateJWT(user.Email)
if err != nil {
c.Status(fiber.StatusBadRequest)
return c.JSON(fiber.Map{
"message": "Invalid credentials",
})
}
cookie := fiber.Cookie{
Name: "access_token",
Value: token,
Expires: time.Now().Add(time.Hour * 24),
HTTPOnly: true,
Secure: true,
}
c.Cookie(&cookie)
return c.JSON(fiber.Map{
"access_token": token,
"token_type": "bearer",
})
}
以上适用于原始JSON
身体,但我想换成form-data
身体
我已经尝试了很多事情,包括这个但无济于事
func Signin(c *fiber.Ctx) error {
type SigninData struct {
email string `json:"email" xml:"email" form:"email"`
password string `json:"password" xml:"password" form:"password"`
}
data := new(SigninData)
if err := c.BodyParser(&data); err != nil {
return err
}
var user models.User
findUser := database.DB.Where("email = ?", data.email).First(&user)
token, err := middlewares.GenerateJWT(user.Email)
if err != nil {
c.Status(fiber.StatusBadRequest)
return c.JSON(fiber.Map{
"message": "Invalid credentials",
})
}
cookie := fiber.Cookie{
Name: "access_token",
Value: token,
Expires: time.Now().Add(time.Hour * 24),
HTTPOnly: true,
Secure: true,
}
c.Cookie(&cookie)
return c.JSON(fiber.Map{
"access_token": token,
"token_type": "bearer",
})
}
但我收到以下错误
schema: interface must be a pointer to struct
我错过了什么,我需要修复它才能接受表单数据?