gin 의 BindJSON 과 ShouldBindJSON,ShouldBindWith 의 차이 점
오 류 를 되 돌려 주 고 header 에 400 의 상태 코드 를 쓰 십시오.
// BindJSON is a shortcut for c.MustBindWith(obj, binding.JSON).
func (c *Context) BindJSON(obj interface{}) error {
return c.MustBindWith(obj, binding.JSON)
}
// MustBindWith binds the passed struct pointer using the specified binding engine.
// It will abort the request with HTTP 400 if any error ocurrs.
// See the binding package.
func (c *Context) MustBindWith(obj interface{}, b binding.Binding) (err error) {
if err = c.ShouldBindWith(obj, b); err != nil {
c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind)
}
return
}
오류 메시지 만 되 돌려 주 고 헤더 에 400 오류 상태 코드 를 쓰 지 않 습 니 다.
// ShouldBindJSON is a shortcut for c.ShouldBindWith(obj, binding.JSON).
func (c *Context) ShouldBindJSON(obj interface{}) error {
return c.ShouldBindWith(obj, binding.JSON)
}
// ShouldBindWith binds the passed struct pointer using the specified binding engine.
// See the binding package.
func (c *Context) ShouldBindWith(obj interface{}, b binding.Binding) error {
return b.Bind(c.Request, obj)
}
func (c *Context) ShouldBindWith(obj interface{}, b binding.Binding) error {
return b.Bind(c.Request, obj)
}
어떻게 선택 하여 사용 합 니까?
gin 은 유연 한 bid 분석 파 라 메 터 를 제공 하여 선택 할 수 있 습 니 다.
// Content-Type
c.Bind(obj interface{})
// binding.JSON,
c.BindJSON(obj interface{})
// ,
c.BindWith(obj interface{}, b binding.Binding)
// Content-Type
c.ShouldBind(obj interface{})
// binding.JSON,
c.ShouldBindJSON(obj interface{})
// ,
c.ShouldBindWith(obj interface{}, b binding.Binding)
4.567917.Shouldxxx 와 bidxxx 의 차 이 는 bidxxx 가 head 에 400 의 반환 정 보 를 추가 하 는 것 이 고 Shouldxxx 는 4.567918 을 추가 하지 않 는 다 는 것 이다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Gin 실행 환경 만들기최근 Google에서 조사하고 있을 때나 SNS에서 자주 보는 Go이지만, 아무래도 웹 프레임워크도 존재하고 있어 표준 라이브러리와는 어떻게 다른지를 볼 수 있도록, 우선은 프레임워크의 하나이다 Gin 패키지를 설치...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.