github.com/GuanceCloud/cliutils@v1.1.21/network/http/http.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  // Package http wraps all HTTP releated common-used utils.
     7  package http
     8  
     9  import (
    10  	"io"
    11  	"net/http"
    12  )
    13  
    14  // ReadBody will automatically unzip body.
    15  func ReadBody(req *http.Request) ([]byte, error) {
    16  	buf, err := io.ReadAll(req.Body)
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  
    21  	// as HTTP server, we do not need to close body
    22  	switch req.Header.Get("Content-Encoding") {
    23  	case "gzip":
    24  		return Unzip(buf)
    25  	default:
    26  		return buf, err
    27  	}
    28  }