Skip to content

Commit dd949d8

Browse files
committed
new method to extract response body
1 parent 89d16a7 commit dd949d8

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

models.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,27 @@ func copyBody(body io.ReadCloser) (resp1, resp2 io.ReadCloser, err error) {
116116
}
117117
return ioutil.NopCloser(&buf), ioutil.NopCloser(bytes.NewReader(buf.Bytes())), nil
118118
}
119+
120+
func extractBody(resp *http.Response) (extract []byte, err error) {
121+
save := resp.Body
122+
savecl := resp.ContentLength
123+
124+
save, resp.Body, err = copyBody(resp.Body)
125+
126+
if err != nil {
127+
return
128+
}
129+
defer resp.Body.Close()
130+
extract, err = ioutil.ReadAll(resp.Body)
131+
132+
resp.Body = save
133+
resp.ContentLength = savecl
134+
if err != nil {
135+
return nil, err
136+
}
137+
return extract, nil
138+
}
139+
119140
// doRequest performs original request and returns response that should be returned to client and error (if there is one)
120141
func (d *DBClient) doRequest(request *http.Request) (*http.Response, error) {
121142
// We can't have this set. And it only contains "/pkg/net/http/" anyway

0 commit comments

Comments
 (0)