GO存储数据返回EOF

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

GO storing data returns EOF

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是golang的新手,正在尝试构建REST API。到目前为止,GET端点对我来说运行良好,但是我在POST方法(创建用户)方面遇到了困难:

这是我的User结构体的样子:

  1. type User struct {
  2. ID uint32 `gorm:"primary_key;auto_increment" json:"id"`
  3. Name string `json:"name" binding:"required"`
  4. Email string `json:"email" binding:"required"`
  5. Password string `json:"password" binding:"required"`
  6. CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
  7. UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
  8. }

存储用户的Repo方法:

  1. func CreateUser() (*models.User, error) {
  2. var input models.User
  3. user := models.User{Name: input.Name, Email: input.Email, Password: input.Password}
  4. result := Config.DB.Debug().Create(&user)
  5. if result.Error != nil {
  6. msg := result.Error
  7. return nil, msg
  8. }
  9. return &user, nil
  10. }

并从控制器调用:

  1. func CreateUser(c *gin.Context) {
  2. //var user models.User
  3. user := models.User{}
  4. user.Prepare()
  5. var input models.User
  6. err := c.BindJSON(&input)
  7. if err != nil {
  8. c.JSON(http.StatusBadRequest, gin.H{
  9. "Error": err.Error(), //this error is thrown
  10. })
  11. return
  12. }
  13. userData, err := repo.CreateUser()
  14. if err != nil {
  15. c.JSON(http.StatusBadRequest, gin.H{
  16. "error": err,
  17. })
  18. return
  19. }
  20. c.JSON(http.StatusOK, gin.H{
  21. "data": userData,
  22. })
  23. }

我正在使用gorm与数据库进行交互,如果我硬编码输入,例如:

  1. User{Name: "Jinzhu", Email: "test@mail.com", Password: "pass1234"}

那么数据会被存储,但是如果通过Postman传递这些参数,我会收到以下错误:

{"Error":"EOF"}

我已经苦思冥想了几个小时,但仍然不明白错误出在哪里。

英文:

I'm new to golang and I'm trying to build rest api, So far GET endpoints are working for me fine, but I'm having difficulties with POST method(creating user):

That's how my User struct is looking:

  1. type User struct {
  2. ID uint32 `gorm:"primary_key;auto_increment" json:"id"`
  3. Name string `json:"name" binding:"required"`
  4. Email string `json:"email" binding:"required"`
  5. Password string `json:"password" binding:"required"`
  6. CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
  7. UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
  8. }

Repo method for storing users:

  1. func CreateUser() (*models.User, error) {
  2. var input models.User
  3. user := models.User{Name: input.Name, Email: input.Email, Password: input.Password}
  4. result := Config.DB.Debug().Create(&user)
  5. if result.Error != nil {
  6. msg := result.Error
  7. return nil, msg
  8. }
  9. return &user, nil
  10. }

And called from controller:

  1. func CreateUser(c *gin.Context) {
  2. //var user models.User
  3. user := models.User{}
  4. user.Prepare()
  5. var input models.User
  6. err := c.BindJSON(&input)
  7. if err != nil {
  8. c.JSON(http.StatusBadRequest, gin.H{
  9. "Error": err.Error(), //this error is thrown
  10. })
  11. return
  12. }
  13. userData, err := repo.CreateUser()
  14. if err != nil {
  15. c.JSON(http.StatusBadRequest, gin.H{
  16. "error": err,
  17. })
  18. return
  19. }
  20. c.JSON(http.StatusOK, gin.H{
  21. "data": userData,
  22. })
  23. }

I'm using gorm to interact with database and if I'm hardcoding the inputs e.g.

  1. User{Name: "Jinzhu", Email: "test@mail.com", Password: "pass1234"}

Then the data is stored, but if these are passed as parameters via postman, then I'm getting this error:

{"Error":"EOF"}

Been bashing my head for several hours now and still don't understand where's the error.

答案1

得分: 1

如mkopriva所指出

c.BindJSON 返回 EOF,这意味着请求中没有发送任何内容

要解决这个问题,你需要在请求体中包含 json,格式应该类似于:

  1. {name: "Jinzhu", email: "test@mail.com", password: "pass1234"}

示例 curl 请求:

  1. curl -X POST localhost:8080 -H 'Content-Type: application/json' --data '{name: "Jinzhu", email: "test@mail.com", password: "pass1234"}'
英文:

As mkopriva pointed

c.BindJSON is returning EOF which means nothing is being sent in request

To solve this you need json in request body which would look something like:

  1. {name: "Jinzhu", email: "test@mail.com", password: "pass1234"}

Example curl request:

  1. curl -X POST localhost:8080 -H 'Content-Type: application/json' --data '{name: "Jinzhu", email: "test@mail.com", password: "pass1234"}'

huangapple
  • 本文由 发表于 2022年4月4日 20:39:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/71737403.html
匿名

发表评论

匿名网友

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

确定