From 611d44108465dd5d1a85293813eb97d7d3f1a1e1 Mon Sep 17 00:00:00 2001 From: Jacek Date: Sun, 19 Feb 2023 22:54:05 +0100 Subject: [PATCH] handle unknown payload --- beam/server.go | 9 +++++++-- frontend/src/components/pages/Beam.vue | 16 ++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/beam/server.go b/beam/server.go index d532b1f..04adcff 100644 --- a/beam/server.go +++ b/beam/server.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "io/ioutil" "net/http" "github.com/wailsapp/wails/v2/pkg/runtime" @@ -19,13 +20,17 @@ type beamRequest struct { } func handleBeam(w http.ResponseWriter, r *http.Request) { - decoder := json.NewDecoder(r.Body) + body, err := ioutil.ReadAll(r.Body) + if err != nil { + runtime.LogErrorf(appCtx, "error reading request body: %s", err) + } var requestData beamRequest - err := decoder.Decode(&requestData) + err = json.Unmarshal(body, &requestData) if err != nil { runtime.LogErrorf(appCtx, "error parsing json: %s", err) + requestData.Payload = string(body) } runtime.EventsEmit(appCtx, "beamMessage", requestData) diff --git a/frontend/src/components/pages/Beam.vue b/frontend/src/components/pages/Beam.vue index 9df09bb..cb34a65 100644 --- a/frontend/src/components/pages/Beam.vue +++ b/frontend/src/components/pages/Beam.vue @@ -21,24 +21,24 @@ function doCopy(data) { } function copyLaravelString() { - const data = `\\Illuminate\\Support\\Facades\\Http::post("http://localhost:${port}/beam", ["payload"=> json_encode(["foo" => "bar"])]);` + const data = `\\Illuminate\\Support\\Facades\\Http::post("http://localhost:${port}/beam", ["payload"=> json_encode(["message" => "Hello World"])]);` doCopy(data) } function copyPHPString() { const data = `file_get_contents('http://localhost:${port}/beam', false, stream_context_create([ - 'http' => ['method' => 'POST', 'header' => 'Content-Type: application/json', - 'content' => json_encode(['payload' => json_encode([ - 'foo' => 'bar', - ])]), - ]]));` + 'http' => ['method' => 'POST', 'header' => 'Content-Type: application/json', + 'content' => json_encode(['payload' => json_encode([ + 'message' => 'Hello World', + ])]), + ]]));` doCopy(data) } function copyGOString() { const data = `client := &http.Client{} req, _ := http.NewRequest("POST", "http://localhost:${port}/beam", bytes.NewBuffer([]byte(\`{ - "Payload": "foo" + "Payload": "{ \\"message\\": \\"Hello World\\"}" }\`))) res, _ := client.Do(req) defer res.Body.Close() @@ -48,7 +48,7 @@ function copyGOString() { function copyCurlString() { - const data = `curl -X POST -d '{"payload": "{ \\\"foo\\\": \\\"bar\\\" }"}' http://localhost:${port}/beam` + const data = `curl -X POST -d '{"payload": "{ \\\"message\\\": \\\"Hello World\\\" }"}' http://localhost:${port}/beam` doCopy(data) }