-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
41 lines (34 loc) · 881 Bytes
/
request.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
package mangoai
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func (m *Mango) DoRequest(endpoint string, method string, payload map[string]interface{}) (map[string]interface{}, error) {
url := fmt.Sprintf("%s/%s", m.baseURL, endpoint)
jsonData, err := json.Marshal(payload)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Error: Report https://github.com/Mishel-07/mangoai/issues")
}
var result map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return nil, err
}
return result, nil
}