#Problems Decoding Body into JSON

10 messages · Page 1 of 1 (latest)

sweet nebula
#

Hi!

I'm building an application font-end in Svelte with a Golang backend and I'm facing some issues decoding a form body into a Golang model; Here is the problem:

The Model:

    gorm.Model
    ClientID int `gorm:"type:integer" json:"client_id"`
    RoomNumber int `gorm:"type:integer" json:"room_number"`
    FechaEntrada time.Time `json:"fecha_entrada"`
    FechaSalida time.Time `json:"fecha_salida"`
    Notas string `json:"notas"`
}```

Here is the body im passing to the Golang handler, here i have a 'Normal' version and  a 'Clean' version i've tried sending after seeing one of the errors: 

Clean version: ```{"client_id":09,"room_number":10,"fecha_entrada":"2000-11-09T00:00:00Z","fecha_salida":"2000-11-10T00:00:00Z","notas":"gran cococ"}```

Normal version: ```{"client_id":"09","room_number":"10","fecha_entrada":"2000-11-09T00:00:00Z","fecha_salida":"2000-11-10T00:00:00Z","notas":"gran cococ"}```

As you can see  the normal one has " surrounding the number fields and the clean version does not, however I get this errors when decoding on the Golang handler:

Normal Version Sent: **json: cannot unmarshal string into Go struct field Registro.client_id of type int**
Clean Version Sent: **invalid character '1' after object key:value pair**

I don't know how to submit the data anymore, I've already formatted the date in order to follow what I belive are go standards... When I try to use this API in postman there is no problem.
oak kindle
#

gorm and json annotations in the same struct

#
type AutoGenerated struct {
    ClientID     string    `json:"client_id"`
    RoomNumber   string    `json:"room_number"`
    FechaEntrada time.Time `json:"fecha_entrada"`
    FechaSalida  time.Time `json:"fecha_salida"`
    Notas        string    `json:"notas"`
}
#

this looks like what you have

#

oh you said it works in postman but not in svelte

#

then it's an issue with how youre generating the json in your frontend

sweet nebula
sweet nebula