无法在 Go Chi 路由器中读取 "request.Body"

Unable to read "request.Body" in Go Chi router

考虑 main/entry 函数中的以下代码

    r := chi.NewRouter()
    r.Use(middleware.RequestID)
    r.Use(middleware.RealIP)
    r.Use(middleware.Logger)
    r.Use(middleware.Recoverer)

    r.Post("/book", controllers.CreateBook)
    http.ListenAndServe(":3333", r)

和 CreateBook 函数定义为

    func CreateBook(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("content-type", "application/json")
    var bookObj models.Book
    err := json.NewDecoder(r.Body).Decode(&bookObj)
    spew.Dump(bookObj)
    collection := db.Client.Database("bookdb").Collection("book")
    ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
    insertResult, err := collection.InsertOne(ctx, bookObj)
    if err != nil {
        log.Fatal(err)
    }
    json.NewEncoder(w).Encode(insertResult)
  }

图书模型

//exporting attributes here solved the issue
type Book struct {
    ID     primitive.ObjectID `json:"id,omitempty" bson:"id,omitempty"`
    name   string             `json:"name,omitempty" bson:"name,omitempty"`
    author string             `json:"author,omitempty" bson:"author,omitempty"`
    isbn   string             `json:"isbn,omitempty" bson:"isbn,omitempty"`
}

但是 json.NewDecoder(r.Body).Decode(&bookObj) 不解析任何东西,因为 req.Body 是空的,没有错误抛出,这是关于 chi 的 Render 函数。

任何人都可以帮助我禁用 chiRenderBind 功能,我只想通过 JSON 解码器解析正文。

导出结构的所有字段解决了这个问题。谢谢@mkopriva

type Book struct {
ID     primitive.ObjectID `json:"id,omitempty" bson:"id,omitempty"`
Name   string             `json:"name,omitempty" bson:"name,omitempty"`
Author string             `json:"author,omitempty" bson:"author,omitempty"`
ISBN   string             `json:"isbn,omitempty" bson:"isbn,omitempty"`

}