added Post helper
This commit is contained in:
32
helper.go
32
helper.go
@@ -1,17 +1,20 @@
|
|||||||
package helper
|
package helper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Get(uri string, authName string, authValue string, queries map[string]string) (*http.Response, error) {
|
func Get(uri string, authName string, authValue string, queries map[string]string) (*http.Response, error) {
|
||||||
client := &http.Client{}
|
|
||||||
|
|
||||||
request, err := http.NewRequest("GET", uri, nil)
|
request, err := http.NewRequest("GET", uri, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
|
||||||
request.Header.Set("Accept", "application/json")
|
request.Header.Set("Accept", "application/json")
|
||||||
request.Header.Set("Authorization", fmt.Sprintf("%s %s", authName, authValue))
|
request.Header.Set("Authorization", fmt.Sprintf("%s %s", authName, authValue))
|
||||||
|
|
||||||
@@ -32,3 +35,28 @@ 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) {
|
||||||
|
jsonBytes, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
body := bytes.NewBuffer(jsonBytes)
|
||||||
|
request, err := http.NewRequest("POST", uri, body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user