From bea6c22195dfdb29bd628fa2a8837788026a7efb Mon Sep 17 00:00:00 2001 From: esterfreyja Date: Wed, 10 Jan 2024 15:23:24 +0100 Subject: [PATCH] first commit --- httpListener.go | 28 ++++++++++++++++++++++++++++ jsonEmitter.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 httpListener.go create mode 100644 jsonEmitter.go diff --git a/httpListener.go b/httpListener.go new file mode 100644 index 0000000..529c394 --- /dev/null +++ b/httpListener.go @@ -0,0 +1,28 @@ +package main + +import ( + "encoding/json" + "fmt" + "net/http" + "log" +) + +type Data struct { + identifier string + password string +} + +func handleRequest(w http.ResponseWriter, r *http.Request) { + var data Data + err := json.NewDecoder(r.Body).Decode(&data) + if err != nil { + http.Error(w,err.Error(), http.StatusBadRequest) + return + } + fmt.Fprintf(w, "Data: %+v", data) +} + +func main() { + http.HandleFunc("/", handleRequest) + log.Fatal(http.ListenAndServe(":8088", nil)) +} diff --git a/jsonEmitter.go b/jsonEmitter.go new file mode 100644 index 0000000..efc0254 --- /dev/null +++ b/jsonEmitter.go @@ -0,0 +1,41 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "log" + "net/http" +) + +type Data struct { + identifier string + password string +} + +func main() { + url := "http://localhost:8088" // Remplacez par l'URL de votre choix + data := Data { + identifier: "machevaldo", + password: "superPassword"} + + jsonData, err := json.Marshal(data) + if err != nil { + log.Fatal(err) + } + + req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) + if err != nil { + log.Fatal(err) + } + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + log.Fatal(err) + } + defer resp.Body.Close() + + fmt.Println("Response Status:", resp.Status) +}