简体   繁体   中英

The request body does not reach golang fiber

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

I think you need to take a look on what is inside this function exactly

task := models.NewList(title, description)

Most probably it has an issue because if the body is not parsed correctly you will not reach the insertion and created-status return statement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM