I am working on writing a server in Go for a project, which involves receiving JSON data from a client and sending back a JSON response. When I run the code, any request I make works correctly, but the response is always empty. Here is the code for my server.
type AddPlayerData struct {
name string
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("[ SUCCESS ] Request from ", r.RemoteAddr)
decoder := json.NewDecoder(r.Body)
var data AddPlayerData
err := decoder.Decode(&data)
if err != nil {
panic(err)
}
defer r.Body.Close()
json.NewEncoder(w).Encode(data)
}).Methods("PUT");
log.Fatal(http.ListenAndServe(":8080", router))
}
The requests I am sending are PUT requests formatted as follows:
{
"name": "test-player"
}
I am receiving a response, but it is always empty.