I have created API in go which takes input id and in response gives user data. Now when I post data to API either Postman or using form, it gives me error invalid character 'i' looking for beginning of value I tried to search and implemented various solution but nothing works.
Router
r.Use(func(c *gin.Context) {
// add header Access-Control-Allow-Origin
c.Writer.Header().Set("Content-Type", "application/json")
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Max-Age", "86400")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, UPDATE")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, X-Max")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(200)
} else {
c.Next()
}
})
v1 := r.Group("/api/v1")
v1.Use(middlewares.UserMiddlewares())
{
v1.POST("user-list", apiControllerV1.UserList)
}
Api Call to function
func UserList(c *gin.Context) {
var userService v1s.UserService
err := json.NewDecoder(c.Request.Body).Decode(&userService.User)
fmt.Println(err)
if err != nil {
u.Respond(c.Writer, u.Message(1, "Invalid request"))
return
}
}
Struct
type Model struct {
ID uint `gorm:"primary_key" json:"id,omitempty"`
CreatedAt time.Time `gorm:"not null" json:"created_at" sql:"DEFAULT:CURRENT_TIMESTAMP"`
UpdatedAt time.Time `gorm:"not null" json:"updated_at" sql:"DEFAULT:CURRENT_TIMESTAMP"`
DeletedAt *time.Time `sql:"index" json:"deleted_at,omitempty"`
}
type User struct {
Model
Name string `gorm:"type:varchar(50)" json:"name" validate:"required"`
Email string `gorm:"type:varchar(50)" json:"email" validate:"required,email"`
}
Posting data
id:1 or {id:1}