From 0064294c3cb0e93783fcacbf73dd5841d9d3801d Mon Sep 17 00:00:00 2001 From: Gregory Wells Date: Fri, 24 Jul 2026 21:09:21 -0700 Subject: [PATCH] simple HTTP request engine --- .gitignore | 2 +- example.config.json | 5 ++++ main.go | 69 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 example.config.json diff --git a/.gitignore b/.gitignore index 6afa617..d344ba6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -auth_data.txt +config.json diff --git a/example.config.json b/example.config.json new file mode 100644 index 0000000..da4c8fc --- /dev/null +++ b/example.config.json @@ -0,0 +1,5 @@ +{ + "url:": "https://mx.example.com", + "username": "admin@example.com", + "password": "password" +} diff --git a/main.go b/main.go index 092bdb4..05e3fea 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,76 @@ package main import ( + "encoding/json" "fmt" + "io" + "log" + "net/http" + "os" + "strings" ) +type Config struct { + URL string `json:"url"` + Username string `json:"username"` + Password string `json:"password"` +} + +var serverConfig Config + +func read_config() { + file, err := os.ReadFile("config.json") + if err != nil { + fmt.Print(err.Error()) + return + } + + err = json.Unmarshal(file, &serverConfig) + if err != nil { + fmt.Print(err.Error()) + } +} + +func convert_username_into_url(username string) string { + result := strings.ReplaceAll(username, "@", "%40") + return result +} + func main() { - fmt.Println("Hello, world from golang") + read_config() + + client := &http.Client{} + + xml := ` + + + + + + ` + + body := strings.NewReader(xml) + + req, _ := http.NewRequest( + "PROPFIND", + serverConfig.URL+"/dav/cal/"+convert_username_into_url(serverConfig.Username), + body, + ) + + req.SetBasicAuth(serverConfig.Username, serverConfig.Password) + req.Header.Set("Depth", "1") + req.Header.Set("Content-Type", "application/xml") + + resp, err := client.Do(req) + if err != nil { + fmt.Print(err.Error()) + } + defer resp.Body.Close() + + body2, err := io.ReadAll(resp.Body) + if err != nil { + log.Fatal(err) + } + + fmt.Println(string(body2)) }