将golang gofiber框架中的REST API的POST请求从JSON转换为form-data。

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

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"`
}

huangapple
  • 本文由 发表于 2021年12月31日 11:42:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/70538796.html
匿名

发表评论

匿名网友

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

确定