英文:
convert rest api POST request from json to form-data in golang gofiber framework
问题
我有以下代码,可以在POST请求的正文中使用JSON
,但现在我想将其转换为在请求的正文中使用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
我错过了什么需要修复的地方,以便使其接受form-data
?
英文:
I have the following code that works for JSON
in the body of the POST request but i now want to convert this to using form-data
in the body of the request
Here is what i have
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",
})
}
above works fine for raw JSON
body but i want to change to form-data
body
I have tried many things including this but to no avail
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",
})
}
but i get the following error
schema: interface must be a pointer to struct
what am i missing that i need to fix to get this to accept form-data?
答案1
得分: 2
BodyParser
方法期望将一个指向结构体的指针作为参数,但是你的代码试图将一个指向指向结构体的指针传递给它。请按照以下方式初始化结构体:
data := SigninData{}
此外,请尝试将SigninData
结构体的字段设置为公共字段:
type SigninData struct {
Email string `json:"email" xml:"email" form:"email"`
Password string `json:"password" xml:"password" form:"password"`
}
英文:
The method BodyParser
expects a pointer to a struct as an argument, but your code is trying to pass it a pointer to a pointer to a struct. Please initialize the struct this way:
data := SigninData{}
Also, try to make the fields of the SigninData
struct public:
type SigninData struct {
Email string `json:"email" xml:"email" form:"email"`
Password string `json:"password" xml:"password" form:"password"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论