apps-durpot/handlers/shared.go

39 lines
768 B
Go
Raw Normal View History

2023-05-27 16:32:59 -05:00
package handlers
import (
2023-06-06 19:13:41 -04:00
"encoding/base64"
2023-05-27 16:32:59 -05:00
"fmt"
2023-05-27 16:43:22 -05:00
"io"
2023-05-27 16:32:59 -05:00
"net/http"
)
2023-06-06 19:13:41 -04:00
func CallDurpAPI(url string, username string, password string) []byte {
2023-05-27 16:32:59 -05:00
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println("Error creating request:", err)
return nil
}
2023-06-06 19:13:41 -04:00
auth := username + ":" + password
basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
req.Header.Set("Authorization", basicAuth)
2023-05-27 16:32:59 -05:00
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return nil
}
defer resp.Body.Close()
2023-05-27 16:43:22 -05:00
body, err := io.ReadAll(resp.Body)
2023-05-27 16:32:59 -05:00
if err != nil {
fmt.Println("Error reading response:", err)
return nil
}
return body
}