3 Commits

Author SHA1 Message Date
Bastian de Byl
3ceb440acc added missing Content-Type to Put and Post 2023-02-26 02:32:20 -05:00
Bastian de Byl
d6c3570ae7 clarified function POST and PUT usage with io.Reader 2023-02-26 02:24:18 -05:00
Bastian de Byl
6ee3c99241 added Put method 2023-02-26 01:57:05 -05:00

View File

@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"net/http" "net/http"
) )
@@ -16,6 +17,7 @@ func Get(uri string, authName string, authValue string, queries map[string]strin
client := &http.Client{} client := &http.Client{}
request.Header.Set("Accept", "application/json") request.Header.Set("Accept", "application/json")
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Authorization", fmt.Sprintf("%s %s", authName, authValue)) request.Header.Set("Authorization", fmt.Sprintf("%s %s", authName, authValue))
if len(queries) > 0 { if len(queries) > 0 {
@@ -36,7 +38,7 @@ func Get(uri string, authName string, authValue string, queries map[string]strin
return response, nil return response, nil
} }
func Post(uri string, authName string, authValue string, data any) (*http.Response, error) { func Post(uri string, authName string, authValue string, data io.Reader) (*http.Response, error) {
jsonBytes, err := json.Marshal(data) jsonBytes, err := json.Marshal(data)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -50,6 +52,32 @@ func Post(uri string, authName string, authValue string, data any) (*http.Respon
client := &http.Client{} client := &http.Client{}
request.Header.Set("Accept", "application/json")
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Authorization", fmt.Sprintf("%s %s", authName, authValue))
response, err := client.Do(request)
if err != nil {
return nil, err
}
return response, nil
}
func Put(uri string, authName string, authValue string, data io.Reader) (*http.Response, error) {
jsonBytes, err := json.Marshal(data)
if err != nil {
return nil, err
}
body := bytes.NewBuffer(jsonBytes)
request, err := http.NewRequest("PUT", uri, body)
if err != nil {
return nil, err
}
client := &http.Client{}
request.Header.Set("Content-Type", "application/json") request.Header.Set("Content-Type", "application/json")
request.Header.Set("Authorization", fmt.Sprintf("%s %s", authName, authValue)) request.Header.Set("Authorization", fmt.Sprintf("%s %s", authName, authValue))