Gin框架編寫PUT接口使用BindJSON綁定參數報錯,當結構體綁定參數有誤時, 狀態碼爲400,強制返回200,結果code還是400.
原因 : BindXXX方法都return c.MustBindWith(obj, binding.XXX)。如果綁定錯誤,則c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind)。這將響應狀態碼設置爲400,並將Content-Type標題設置爲text/plain; charset=utf-8。如果強制修改成200,就會警告[WARNING] Headers were already written. Wanted to override status code 400 with 200。
解決方法 : 綁定結構體的方法把BindJSON改爲ShouldBind。