我構建了一個 echo 微服務 api,有兩個路由:post 和 get。get 方法工作正常,但 get 方法無法解析 JSON,這意味著在 Bind() func 之后結構為空。這一定是我想念的一件非常愚蠢和微小的事情......有什么幫助嗎?// main.go//--------------------------------------------------------------------func main() { e := echo.New() e.GET("/getmethod", func(c echo.Context) error { return c.JSON(200, "good")}) e.POST("/login", handlers.HandleLogin) e.Start("localhost:8000")}// handlers/login.go//--------------------------------------------------------------------type credentials struct { email string `json:"email"` pass string `json:"pass"`}//--------------------------------------------------------------------func HandleLogin(c echo.Context) error { var creds credentials err := c.Bind(&creds) if err != nil { return c.JSON(http.StatusBadRequest, err) // 400 } return c.JSON(http.StatusOK, creds.email) // 200}當使用郵遞員運行發布請求時(確保:發布方法,url 是正確的路由,在 body> raw> JSON 格式下,我按預期發送 JSON)我收到返回狀態 200 ok,但 json 為空,而我希望收到電子郵件屬性。知道為什么 Bind() 沒有正確提取字段嗎?
1 回答

慕后森
TA貢獻1802條經驗 獲得超5個贊
您應該通過將每個首字母大寫來導出憑證結構的字段,否則 json-package 不知道您有哪些字段:
type?credentials?struct?{ ????Email?string?`json:"email"` ????Pass?string?`json:"pass"`}
- 1 回答
- 0 關注
- 114 瀏覽
添加回答
舉報
0/150
提交
取消