[Golang - gin framework] `ShouldBindJSON` , `BindJSON` 차이점steemCreated with Sketch.

in kr-dev 커뮤니티last year

ShouldBindJSON , BindJSON 차이점

ShouldBindJSON 은 개발자가 바인딩을 직접 해야하는 메서드이다.
BindJSON 는 이미 바인딩이 된 메서드이다. 에러 발생시 http status 400 으로 전달된다.

ShouldBindJSON

// ShouldBindJSON is a shortcut for c.ShouldBindWith(obj, binding.JSON).
func (c *Context) ShouldBindJSON(obj interface{}) error {
    return c.ShouldBindWith(obj, binding.JSON)
}

BindJSON

// 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 occurs.
// See the binding package.
func (c *Context) MustBindWith(obj interface{}, b binding.Binding) error {
    if err := c.ShouldBindWith(obj, b); err != nil {
        c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind) // nolint: errcheck
        return err
    }
    return nil
}

https://github.com/gin-gonic/gin#bind-query-string-or-post-data

Sort:  
 last year 

[광고] STEEM 개발자 커뮤니티에 참여 하시면, 다양한 혜택을 받을 수 있습니다.

Upvoted! Thank you for supporting witness @jswit.

Coin Marketplace

STEEM 0.29
TRX 0.12
JST 0.034
BTC 62759.93
ETH 3112.27
USDT 1.00
SBD 3.87