github.com/gogf/gf/v2@v2.7.4/net/ghttp/ghttp_middleware_json_body.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package ghttp
     8  
     9  import (
    10  	"github.com/gogf/gf/v2/internal/json"
    11  )
    12  
    13  // MiddlewareJsonBody validates and returns request body whether JSON format.
    14  func MiddlewareJsonBody(r *Request) {
    15  	requestBody := r.GetBody()
    16  	if len(requestBody) > 0 {
    17  		if !json.Valid(requestBody) {
    18  			r.SetError(ErrNeedJsonBody)
    19  			return
    20  		}
    21  	}
    22  	r.Middleware.Next()
    23  }