I’m new to go. I’m liking go however I’m loosing my thoughts over this half.
Right here is the related go code. All it does is ship a tcp string command.
kind CasparRequest struct {
Command string
}
mux.HandleFunc("/casparconnecting", func(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
connection, err := amcp.Dial("127.0.0.1:5250")
if err != nil {
// log.Deadly(err)
w.Header().Set("Content material-Kind", "software/json")
w.WriteHeader(http.StatusBadRequest)
}
if connection == nil {
w.Header().Set("Content material-Kind", "software/json")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Connection has not been established"))
return
}
defer connection.Shut()
var command CasparRequest
err1 := json.NewDecoder(req.Physique).Decode(&command)
if err1 != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Println("the command despatched", command.Command)
code, replyData, err := connection.Do(command.Command)
if err != nil {
log.Deadly(err)
w.Header().Set("Content material-Kind", "software/json")
w.WriteHeader(http.StatusBadRequest)
}
fmt.Println("replyCode", code)
fmt.Println("replyCode", replyData)
replyString := fmt.Sprintf("%v", replyData)
w.Header().Set("Content material-Kind", "software/json")
w.WriteHeader(http.StatusOK) // Set your code right here, after setting your headers
w.Write([]byte(replyString))
// defer c.Shut()
})
The next curl request works:curl --header "Content material-Kind: software/json" --request POST --data '{"command":"PLAY 1-1 myfile000.mp4"}' http://localhost:8080/casparconnecting
Nonetheless as quickly as I do a request from the browser the server panicks
const fetched = await fetch("http://localhost:8080/casparconnecting", {
headers: {
"Content material-Kind": "software/json",
},
technique: "POST",
physique: JSON.stringify({ command: "PLAY 1-1 myfile000.mp4" }),
});
That is the main points of javascript POST request
{
"args": {},
"knowledge": "{"command":"PLAY 1-1 myfile000.mp4"}",
"recordsdata": {},
"type": {},
"headers": {
"Settle for": "*/*",
"Settle for-Encoding": "gzip, deflate, br, zstd",
"Settle for-Language": "en-GB,en-US;q=0.9,en;q=0.8",
"Connection": "keep-alive",
"Content material-Size": "36",
"Content material-Kind": "software/json",
"Host": "localhost:9090",
"Origin": "http://localhost:5173",
"Referer": "http://localhost:5173/",
"Sec-Ch-Ua": ""Chromium";v="128", "Not;A=Model";v="24", "Google Chrome";v="128"",
"Sec-Ch-Ua-Cell": "?0",
"Sec-Ch-Ua-Platform": ""Linux"",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Website": "same-site",
"Person-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
},
"json": {
"command": "PLAY 1-1 myfile000.mp4"
},
"technique": "POST",
"origin": "172.17.0.1",
"url": "http://localhost:9090/something"
}
That is the main points of the curl request:
{
"args": {},
"knowledge": "{"command":"play 1-1 myfile000.mp4"}",
"recordsdata": {},
"type": {},
"headers": {
"Settle for": "*/*",
"Content material-Size": "36",
"Content material-Kind": "software/json",
"Host": "localhost:9090",
"Person-Agent": "curl/7.81.0"
},
"json": {
"command": "play 1-1 myfile000.mp4"
},
"technique": "POST",
"origin": "172.17.0.1",
"url": "http://localhost:9090/something"
}
why is javascript request inflicting a difficulty right here. Even when I copy the request as curl from the browser it really works. However then I don’t perceive how is that this inflicting an error on the backend.
That is how my go code fails
2024/09/04 12:58:22 http: panic serving [::1]:34408: runtime error: invalid reminiscence tackle or nil pointer dereference
goroutine 6 [running]:
internet/http.(*conn).serve.func1()
/usr/native/go/src/internet/http/server.go:1898 +0xbe
panic({0x6b2360?, 0x93e4f0?})
/usr/native/go/src/runtime/panic.go:770 +0x132
important.important.func3({0x785120, 0xc0000e6620}, 0xc0000ca360)
/residence/john_doe/repos/basketball-backend-go/important.go:139 +0x7f5
internet/http.HandlerFunc.ServeHTTP(0xc0000bab60?, {0x785120?, 0xc0000e6620?}, 0x651dda?)
/usr/native/go/src/internet/http/server.go:2166 +0x29
internet/http.(*ServeMux).ServeHTTP(0x467db9?, {0x785120, 0xc0000e6620}, 0xc0000ca360)
/usr/native/go/src/internet/http/server.go:2683 +0x1ad
internet/http.serverHandler.ServeHTTP({0xc0000b8cf0?}, {0x785120?, 0xc0000e6620?}, 0x6?)
/usr/native/go/src/internet/http/server.go:3137 +0x8e
internet/http.(*conn).serve(0xc0000fe000, {0x7856b0, 0xc0000b8c00})
/usr/native/go/src/internet/http/server.go:2039 +0x5e8
created by internet/http.(*Server).Serve in goroutine 1
/usr/native/go/src/internet/http/server.go:3285 +0x4b4
Please assist. I don’t perceive the place it could possibly be going flawed.