mirror of
				https://github.com/songquanpeng/one-api.git
				synced 2025-11-04 15:53:42 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			27 lines
		
	
	
		
			460 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			27 lines
		
	
	
		
			460 B
		
	
	
	
		
			Go
		
	
	
	
	
	
package common
 | 
						|
 | 
						|
import (
 | 
						|
	"bytes"
 | 
						|
	"encoding/json"
 | 
						|
	"github.com/gin-gonic/gin"
 | 
						|
	"io"
 | 
						|
)
 | 
						|
 | 
						|
func UnmarshalBodyReusable(c *gin.Context, v any) error {
 | 
						|
	requestBody, err := io.ReadAll(c.Request.Body)
 | 
						|
	if err != nil {
 | 
						|
		return err
 | 
						|
	}
 | 
						|
	err = c.Request.Body.Close()
 | 
						|
	if err != nil {
 | 
						|
		return err
 | 
						|
	}
 | 
						|
	err = json.Unmarshal(requestBody, &v)
 | 
						|
	if err != nil {
 | 
						|
		return err
 | 
						|
	}
 | 
						|
	// Reset request body
 | 
						|
	c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
 | 
						|
	return nil
 | 
						|
}
 |