I'm trying to make an API endpoint which would save stuff into a database however, the integers which I pass in via POST request don't seem to be parsing correctly
Here's my structs:
type OnlineTestForm struct {
Form OnlineTestSet `json:"form"`
}
type OnlineTestSet struct {
ID int `db:"id" json:"id"`
OnlineTestSubjectId int `db:"online_test_subject_id" json:"online_test_subject_id"`
Name string `db:"name" json:"name"`
ScoreToPass int `db:"score_to_pass" json:"score_to_pass"`
TimeLimit int `db:"time_limit" json:"time_limit"`
Description string `db:"description" json:"description"`
Enabled bool `db:"enabled" json:"enabled"`
Online bool `db:"online" json:"online"`
TestType string `db:"test_type" json:"test_type"`
DocName string `db:"doc_name" json:"doc_name"`
}
And the "problematic" function where I parse the JSON into a struct:
func NewOnlineTest(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var jsonForm OnlineTestForm
json.NewDecoder(r.Body).Decode(&jsonForm)
err := addNewOnlineTest(jsonForm.Form)
if err != nil {
fmt.Println(err)
w.WriteHeader(500)
fmt.Fprint(w, "{\"error\": {\"message\": \"An error occured while adding new test - "+err.Error()+"\"}}")
return
}
w.WriteHeader(200)
}
I'm using httprouter for as my router and the route to which I post is defined like this
router.POST("/onlinetest/new", NewOnlineTest)
And finally the test POST request which I send has the following payload:
{
"form":{
"name":"test",
"test_type":"document",
"score_to_pass":32,
"time_limit":324,
"enabled":true,
"online":true,
"online_test_subject_id":1
}
}
The problem occurs when I try to use jsonForm.Form' but the integers I've passed in like
time_limitand
score_to_pass` are 0
{0 0 test 0 0 true true document []}