Problems when creating a record with Go and Postgresql from GORM Data type time.Time

0

When I try to send information from POSTMAN, I get an error with dates

Error Line: err: = json.NewDecoder (r.Body) .Decode (& amp)

ERROR:

{"message": "Error reading the data of the entities to be registered: parsing time \" 2012-12-02 \ "as \" 2006-01-02T15: 04: 05.999999-07: 00 \ ": can not parse \ "\" as \ "T \" "," code ": 400}

func EnteCreate (w http.ResponseWriter, r * http.Request) {     entity: = models.Ente {}     m: = models.Message {}     user: = models.User {}

user, _ = r.Context().Value("user").(models.User)

err := json.NewDecoder(r.Body).Decode(&ente)
if err != nil {
    m.Message = fmt.Sprintf("Error al leer los datos de los entes a registrar: %s", err)
    m.Code = http.StatusBadRequest
    commons.DisplayMessage(w, m)
    return
}

ente.UserID = user.ID

db := configuration.GetConnection()
defer db.Close()

err = db.Create(&ente).Error
if err != nil {
    m.Message = fmt.Sprintf("Error al  crear el registro: %s", err)
    m.Code = http.StatusBadRequest
    commons.DisplayMessage(w, m)
    return
}

m.Message = "Ente creado con éxito"
m.Code = http.StatusOK
commons.DisplayMessage(w, m)

}

    
asked by ovasquezbrito 05.06.2018 в 15:33
source

1 answer

0

The problem is when trying to parse the JSON date to the time of golang which expects the format RFC3339, the simplest solution is to send the date in the expected format that is the one that shows you the error "2006-01 -02T15: 04: 05.999999-07: 00 "and not the one you are sending" 2006-01-02 ". If this is not possible then you have to create your own data type and implement the MarshalJSON and the UnmarshalJSON of this type of data to be used in your model. Here you can see an answer to a similar problem link

    
answered by 01.10.2018 в 17:14