I have updated gin-gonic and want to use it to save a form.
Here is my code:
import (
"fmt"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
...
func HandlePost(c *gin.Context) {
userId, userName := getUserId(c)
title := c.PostForm("title")
content := c.PostForm("content")
fmt.Println("userId is:", userId) //ok
fmt.Println("userName is: ", userName) //ok
fmt.Println("title is:", title) //empty
fmt.Println("content is: ", content) //empty
...
I use Postman to post form, like this:
I don't get any errors but received form fields are empty. So wondering what could be wrong here and how to fix it?
Update: here is how I implemented getUserId
:
func getUserId(c *gin.Context) (int, string) {
var user []model.User
session := sessions.Default(c)
userEmail := session.Get("user-id").(string)
if userEmail == "" {
fmt.Println("userEmail NOT found")
} else {
err := shared.Dbmap.Select(&user, "SELECT * FROM user WHERE email = ? LIMIT 1", userEmail)
if err != nil {
log.Panic(err)
}
}
return user[0].Id, user[0].UserName
}