绑定请求方法POST

我绑定请求时遇到问题,因为param很多,所以我使用struct contains param。

package api
import (
    "github.com/labstack/echo/v4"
    "net/http"
    "trains-api/domain/models"
    "trains-api/domain/services"
)

type reqCreate struct {
    RequestNotifi  models.ResquestCreateNotifi  
}
func CreateNotification (c echo.Context) error {
    req := reqCreate{}

    if err := c.Bind(req); err != nil {
        return c.JSON(http.StatusnotFound,err)
    }
}
package models
type RequestCreateNotifi struct {
Name_param1     string    `db:"Name_param1"`
Name_param2     string    `db:"Name_param2"`
....
Name_param_n    string    `db:"Name_param n"`
}

if err := c.Bind(req); err != nil处的错误

r = {interface {} | string } "reflect: Elem of invalid type"
aaaaaap 回答:绑定请求方法POST

您需要像这样设置模型中每个字段的JSON等效项:

package models
type RequestCreateNotifi struct {
    Name_param1     string    `json:"name_param1" db:"Name_param1"`
    Name_param2     string    `json:"name_param2" db:"Name_param2"`
    ....
    Name_param_n    string    `json:"name_param_n" db:"Name_param n"`
}

此json字段指定该字段在请求中的表示方式,以便可以将其绑定到正确的值。

本文链接:https://www.f2er.com/3097837.html

大家都在问