0

When I send a POST request, the server does not receive the request body. Only the id is added to the database

"package lists

import (
"github.com/fishkaoff/fiber-todo-list/pkg/common/models"

"github.com/gofiber/fiber/v2"
)

type AddTaskRequestBody struct {
    title string `json:"title"`
    description string `json:"description"`
}

func (h handler) Addtask(c *fiber.Ctx) error {
    body := AddTaskRequestBody{}

    if err := c.BodyParser(&body); err != nil {
        return fiber.NewError(fiber.StatusBadRequest, err.Error())
    }

    title := body.title
    description := body.title
    if title == "" || description == "" {
        return fiber.NewError(fiber.StatusBadRequest)
    }
    task := models.NewList(title, description)

    if result := h.DB.Create(&task); result.Error != nil {
        return fiber.NewError(fiber.StatusNotFound, 
    }
    return c.Status(fiber.StatusCreated).JSON(&task)
}"

postman request: enter image description here

fishkaoff
  • 19
  • 3

1 Answers1

0

the fields of the structure AddTaskRequestBody are not being exported. That may be the problem. Shouldn't title and description be uppercase - (https://golangbyexample.com/exported-unexported-fields-struct-go/)

type AddTaskRequestBody struct { title string json:"title" description string json:"description" }

Manoj Guglani
  • 134
  • 1
  • 11
  • Thanks. It not solved my problem. But You have guided me to the right path. I checked my models.go and everything was there with a small letter and because of this, fields with the data I needed were not created in the database. – fishkaoff Aug 19 '22 at 09:43