-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.go
More file actions
46 lines (40 loc) · 1023 Bytes
/
request.go
File metadata and controls
46 lines (40 loc) · 1023 Bytes
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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)
type RequestResponse struct {
StatusCode int
Status string
RespBody []byte
}
func DoPostRequest(url string, body interface{}, headers map[string]string) (RequestResponse, LocalError) {
bodyJSON, err := json.Marshal(body)
if err != nil {
return RequestResponse{}, RequestMarshalErr.WithError(err)
}
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(bodyJSON))
for key, val := range headers {
req.Header.Set(key, val)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return RequestResponse{}, RequestExecErr.WithError(err)
}
defer resp.Body.Close()
responseBody, _ := ioutil.ReadAll(resp.Body)
reqResp := RequestResponse{
StatusCode: resp.StatusCode,
Status: resp.Status,
RespBody: responseBody,
}
if resp.StatusCode != 200 {
respErr := RequestRespWarn.WithError(nil)
respErr.SetAdditionalDetails(string(responseBody))
return reqResp, respErr
}
return reqResp, nil
}