Compare commits
4 Commits
file_uploa
...
b475da66da
Author | SHA1 | Date | |
---|---|---|---|
b475da66da | |||
d0085ab2c3 | |||
58514f4c5f | |||
2a32a1b3ce |
2
go.mod
2
go.mod
@ -4,5 +4,5 @@ go 1.20
|
||||
|
||||
require (
|
||||
github.com/lib/pq v1.10.9
|
||||
golang.org/x/crypto v0.8.0
|
||||
golang.org/x/crypto v0.11.0
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@ -1,4 +1,4 @@
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
|
||||
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
|
||||
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
|
||||
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
|
||||
|
57
restclient/client.go
Normal file
57
restclient/client.go
Normal file
@ -0,0 +1,57 @@
|
||||
package restclient
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// SendRequest sends an HTTP request to a URL and includes the specified headers and body.
|
||||
// A body can be nil for GET requests, a map[string]string for multipart/form-data requests,
|
||||
// or a struct for JSON requests
|
||||
func SendRequest(url string, method string, headers map[string]string, body interface{}) (http.Response, error) {
|
||||
var reqBody *bytes.Buffer
|
||||
var contentType string
|
||||
|
||||
switch v := body.(type) {
|
||||
case nil:
|
||||
reqBody = bytes.NewBuffer([]byte(""))
|
||||
case map[string]string:
|
||||
reqBody = &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(reqBody)
|
||||
for key, value := range v {
|
||||
writer.WriteField(key, value)
|
||||
}
|
||||
writer.Close()
|
||||
contentType = writer.FormDataContentType()
|
||||
default:
|
||||
jsonBody, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return http.Response{}, err
|
||||
}
|
||||
reqBody = bytes.NewBuffer(jsonBody)
|
||||
contentType = "application/json"
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, url, reqBody)
|
||||
if err != nil {
|
||||
return http.Response{}, err
|
||||
}
|
||||
|
||||
if contentType != "" {
|
||||
req.Header.Set("Content-Type", contentType)
|
||||
}
|
||||
|
||||
for key, value := range headers {
|
||||
req.Header.Add(key, value)
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return http.Response{}, err
|
||||
}
|
||||
|
||||
return *resp, nil
|
||||
}
|
Reference in New Issue
Block a user