-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
46 lines (36 loc) · 986 Bytes
/
client.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
package grpc_captcha_validation
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
func clientRequest(address, method string, parameter map[string]string, headers map[string]string, response any) error {
client := http.DefaultClient
req, err := http.NewRequest(method, address, nil)
if err != nil {
return ERR_FAILED_CREATE_REQUEST
}
query := url.Values{}
for k, v := range parameter {
query[k] = []string{v}
}
req.URL.RawQuery = query.Encode()
for k, v := range headers {
req.Header.Add(k, v)
}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("secret: failed http do request, got error %s", err.Error())
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("secret: failed to read body, got error %s", err.Error())
}
if err := json.Unmarshal(b, response); err != nil {
return fmt.Errorf("secret: failed to unmarshal data to response, got error %s", err.Error())
}
return nil
}