Toolbox/goDataverseStrict/connect/connect.go

174 lines
3.2 KiB
Go

package connect
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
func GetRequest(url string, params, header map[string]string) (b []byte, err error, debug string) {
client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
for k, v := range header {
req.Header.Add(k, v)
}
query := req.URL.Query()
for k, v := range params {
query.Add(k, v)
}
req.URL.RawQuery = query.Encode()
//DEBUG
//fmt.Println(req.URL.String())
debug = req.URL.String()
//def.LogMessage("GetRequest(req.URL.String())", req.URL.String(), def.DEF_logdebg)
res, _ := client.Do(req)
//DEBUG
//fmt.Println(res.Request.Header)
//def.LogMessage("GetRequest(req.URL.String())", req.URL.String(), def.DEF_logdebg)
b, err = ioutil.ReadAll(res.Body)
res.Body.Close()
return
}
func PostRequest(url string, params, header map[string]string) (b []byte, err error, debug string) {
client := &http.Client{}
postData, err := json.Marshal(params)
//fmt.Println(postData)
req, _ := http.NewRequest("POST", url, bytes.NewReader(postData))
if err != nil {
log.Fatal(err)
}
for k, v := range header {
req.Header.Add(k, v)
}
//DEBUG
//fmt.Println(req.URL.String())
debug = req.URL.String()
res, _ := client.Do(req)
//DEBUG
//fmt.Println(res.Request.Header)
b, err = ioutil.ReadAll(res.Body)
res.Body.Close()
return
}
func PostRequestB(url string, postData []byte, header map[string]string) (b []byte, err error, debug string) {
client := &http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewReader(postData))
if err != nil {
log.Fatal(err)
}
for k, v := range header {
req.Header.Add(k, v)
}
//DEBUG
//fmt.Println(req.URL.String())
debug = req.URL.String()
res, _ := client.Do(req)
//DEBUG
//fmt.Println(res.Request.Header)
b, err = ioutil.ReadAll(res.Body)
res.Body.Close()
return
}
func PutRequest(url, body string, header map[string]string) (b []byte, err error, debug string) {
client := &http.Client{}
req, _ := http.NewRequest("PUT", url, bytes.NewReader([]byte(body)))
if err != nil {
log.Fatal(err)
}
for k, v := range header {
req.Header.Add(k, v)
}
//DEBUG
//fmt.Println(req.URL.String())
debug = req.URL.String()
res, _ := client.Do(req)
//DEBUG
//fmt.Println(res.Request.Header)
b, err = ioutil.ReadAll(res.Body)
res.Body.Close()
return
}
func PutRequestB(url string, body []byte, header map[string]string) (b []byte, err error, debug string) {
client := &http.Client{}
req, err := http.NewRequest("PUT", url, bytes.NewReader(body))
if err != nil {
log.Fatal(err)
}
for k, v := range header {
req.Header.Add(k, v)
}
//DEBUG
//fmt.Println(req.URL.String())
debug = req.URL.String()
res, _ := client.Do(req)
//DEBUG
//fmt.Println(res.Request.Header)
b, err = ioutil.ReadAll(res.Body)
res.Body.Close()
return
}
func DeleteRequest(url string, header map[string]string) (b []byte, err error, debug string) {
client := &http.Client{}
req, _ := http.NewRequest("DELETE", url, nil)
for k, v := range header {
req.Header.Add(k, v)
}
//DEBUG
//fmt.Println(req.URL.String())
debug = req.URL.String()
res, _ := client.Do(req)
//DEBUG
//fmt.Println(res.Request.Header)
b, err = ioutil.ReadAll(res.Body)
res.Body.Close()
return
}