Skip to content

Commit ac76cef

Browse files
committed
fix empty-body handling
1 parent f5e928a commit ac76cef

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

utils/request/request.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ func GetBody(req *http.Request) ([]byte, error) {
8888

8989
if err != nil {
9090
log.Error("Could not read Body: ", err.Error())
91+
92+
req.Body.Close()
93+
9194
return nil, err
9295
}
9396
defer req.Body.Close()
@@ -97,14 +100,20 @@ func GetBody(req *http.Request) ([]byte, error) {
97100

98101
func GetReqBody(w http.ResponseWriter, req *http.Request) Body {
99102
bytes, err := GetBody(req)
103+
104+
var isEmpty bool
105+
106+
isEmpty = len(bytes) > 0
107+
108+
if isEmpty {
109+
return Body{Empty: true}
110+
}
100111

101112
if err != nil {
102113
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
103-
req.Body.Close()
104114

105115
return Body{Empty: true}
106116
}
107-
defer req.Body.Close()
108117

109118
var data map[string]interface{}
110119

@@ -123,7 +132,7 @@ func GetReqBody(w http.ResponseWriter, req *http.Request) Body {
123132
}
124133
}
125134

126-
isEmpty := len(data) <= 0
135+
isEmpty = len(data) <= 0
127136

128137
return Body{
129138
Raw: bytes,

0 commit comments

Comments
 (0)