I have a simple server written in Go:
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"io/ioutil"
"net/http"
)
type Game struct {
RID string `json: "RID"`
Country string `json: "Country"`
}
func postWaitingGames(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Println(err)
}
var game Game
err = json.Unmarshal(body, &game)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%v\n", game)
defer r.Body.Close()
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/wait/", postWaitingGames).Methods("POST")
http.ListenAndServe(":8081", router)
}
And a simple client written in Python for testing purposes. Here's the code:
import json
import requests
json_to_send = json.dumps({"RID": "8", "Country": "Australia"})
post_headers = {'Content-type': 'application/json'}
addr = "http://127.0.0.1:8081/wait/"
resp = requests.post(url=addr, json=json_to_send, headers=post_headers)
print(resp.status_code)
and everytime the client hits the server, the latter yields this error:
json: cannot unmarshal string into Go value of type main.Game
I'm aware of Handling JSON Post Request in Go
Python version == 3.4 Go version == 1.7
Thank you in advance.