我知道有很多人遇到了同样的问题,但我仍然在这里。我很确定我的代码是正确的,但生成的结构仍然是空的。功能:
func PostAdminHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "application/json")
var admin admin.Admin
json.NewDecoder(r.Body).Decode(&admin)
fmt.Println(admin)
_, err := PostAdmin(admin)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
控制台打印 :
{ ObjectID("000000000000000000000000")}
结构:
package entity
import "go.mongodb.org/mongo-driver/bson/primitive"
type Admin struct {
FirstName string
LastName string
Email string
Password string
Role string
Campus primitive.ObjectID
}
路线:
adminRoute.HandleFunc("/admin", admin.PostAdminHandler).Methods("POST")
我通过失眠发送的Json数据:
{
"FirstName": "Jeanne",
"LastName": "Darc",
"Email": "jeanne.darc@rouen.fr",
"Password": "JeanneDarc2022",
"Role": "admin",
"Campus": "60d5a25ff4d722d3b77d1929",
}
错误我从解码器得到:
invalid character '}' looking for beginning of object key string
慕森卡
相关分类