/
http_requests.go
59 lines (48 loc) · 1.53 KB
/
http_requests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package http_utils
import (
"bytes"
"github.com/pyneda/sukyan/db"
"github.com/rs/zerolog/log"
"io"
"net/http"
"strings"
)
// BuildRequestFromHistoryItem gets a history item and returns an http.Request with the same data
func BuildRequestFromHistoryItem(historyItem *db.History) (*http.Request, error) {
var body io.Reader
method := strings.ToUpper(historyItem.Method)
if historyItem.RequestBody != nil {
body = bytes.NewReader(historyItem.RequestBody)
}
request, err := http.NewRequest(method, historyItem.URL, body)
if err != nil {
log.Info().Err(err).Msg("Error creating the request")
return nil, err
}
SetRequestHeadersFromHistoryItem(request, historyItem)
return request, nil
}
// SendRequest sends an http request and returns the response ensuring that the Request body is still readable so we can dump it
func SendRequest(client *http.Client, req *http.Request) (*http.Response, error) {
var bodyCopy io.ReadCloser
if req.Body != nil {
// Create copy of the body
bodyBytes, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}
// Ensure the original body can be read again
req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
// Create a copy for after the Do
bodyCopy = io.NopCloser(bytes.NewBuffer(bodyBytes))
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
// After the client.Do call, replace the request body in the response
// This is done so that when you get the request from response.Request,
// it still has its body
resp.Request.Body = bodyCopy
return resp, err
}