Skip to content

Commit

Permalink
handle unknown payload
Browse files Browse the repository at this point in the history
  • Loading branch information
jsawo committed Feb 19, 2023
1 parent 1f356fe commit 611d441
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
9 changes: 7 additions & 2 deletions beam/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"net/http"

"github.com/wailsapp/wails/v2/pkg/runtime"
Expand All @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions frontend/src/components/pages/Beam.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}
Expand Down

0 comments on commit 611d441

Please sign in to comment.