|
| 1 | +package utility |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + log "github.com/sirupsen/logrus" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | +) |
| 10 | + |
| 11 | +func parseJSONRespo(respJSON []byte, structType interface{}) { |
| 12 | + if respJSON != nil { |
| 13 | + json.Unmarshal(respJSON, &structType) |
| 14 | + } |
| 15 | + return |
| 16 | +} |
| 17 | + |
| 18 | +// FireRequest fires the request based on the parameters to the provided URL |
| 19 | +func FireRequest(payloadJSON string, url string, reqHeaders map[string]string, method string) ([]byte, error) { |
| 20 | + var req *http.Request |
| 21 | + |
| 22 | + if len(payloadJSON) > 0 { |
| 23 | + req, err = http.NewRequest(method, url, bytes.NewBuffer([]byte(payloadJSON))) |
| 24 | + log.Debugf("JSON String getting passed as payload: %s to the URL %s", payloadJSON, url) |
| 25 | + req.Header.Set("Content-Type", "application/json") |
| 26 | + } else { |
| 27 | + log.Debug("No payload to pass") |
| 28 | + req, err = http.NewRequest(method, url, nil) |
| 29 | + } |
| 30 | + for key, val := range reqHeaders { |
| 31 | + req.Header.Set(key, val) |
| 32 | + } |
| 33 | + client := &http.Client{} |
| 34 | + resp, err := client.Do(req) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + defer resp.Body.Close() |
| 39 | + body, _ := ioutil.ReadAll(resp.Body) |
| 40 | + //log.Debugf("Response body getting returned: %s", string(body)) |
| 41 | + return body, nil |
| 42 | +} |
0 commit comments