gin 의 BindJSON 과 ShouldBindJSON,ShouldBindWith 의 차이 점

9171 단어 gingolang
gin 의 BindJSON 과 ShouldBindJSON,ShouldBindWith 의 차이 점
  • BindJSON 과 ShouldBindJSON,ShouldBindWith 의 차이 점
  • 어떻게 사용 할 것 인가BindJSON 과 ShouldBindJSON,ShouldBindWith 의 차이 점
  • BindJSON()

  • 오 류 를 되 돌려 주 고 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
    }
    
  • ShouldBindJSON()

  • 오류 메시지 만 되 돌려 주 고 헤더 에 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)
    }
    
  • ShouldBindWith,b 의 유형 에 따라 json,query 를 연결 하고 연결 합 니 다
  • func (c *Context) ShouldBindWith(obj interface{}, b binding.Binding) error {
    	return b.Bind(c.Request, obj)
    }
    

    어떻게 선택 하여 사용 합 니까?
    gin 은 유연 한 bid 분석 파 라 메 터 를 제공 하여 선택 할 수 있 습 니 다.
  • 오 류 를 헤 더 에 400 의 상태 코드 를 쓰 십시오
  • 	//    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 을 추가 하지 않 는 다 는 것 이다.

    좋은 웹페이지 즐겨찾기