I am making a a roulette REST API in Golang:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/gorilla/mux"
)
func handleRequests() {
// creates a new instance of a mux router
myRouter := mux.NewRouter().StrictSlash(true)
myRouter.HandleFunc("/spin/", handler).Methods("POST")
log.Fatal(http.ListenAndServe(":10000", myRouter))
}
func handler(w http.ResponseWriter, r *http.Request) {
reqBody, _ := ioutil.ReadAll(r.Body)
s := string(reqBody)
fmt.Println(s)
}
func main() {
fmt.Println("Rest API v2.0 - Mux Routers")
handleRequests()
}
main.go
And I am testing the POST method with a Python script:
import requests
url = 'http://localhost:10000/spin/'
myobj = {'bets':[
{
'amount' : 10,
'position' : [0,1,2]
},
{
'amount' : 20,
'position' : [10]
}
]
}
x = requests.post(url, data = myobj)
print(x.text)
test.py
When I run the test script. And the server receives my POST request. The request body is:
bets=amount&bets=position&bets=amount&bets=position
The problem is that the values for the 'amount' and 'position' keys are not there.
My question is - how can I make/ handle POST requests so that I am able to access the values for the embedded keys 'amount' and 'position' in my handler function on the Go server, so that I can put this information into an instance of a struct.