Hi there I back to ask again sorry if you guys think I lack of searching but been doing this for hours and I still don't get it why
I have a controller like this in Golang :
c3 := router.CreateNewControllerInstance("index", "/v4")
c3.Post("/insertEmployee", insertEmployee)
and using Postman to throw this arraylist request , the request is like this
[{
"IDEmployee": "EMP4570787",
"IDBranch": "ALMST",
"IDJob": "PRG",
"name": "Mikey Ivanyushin",
"street": "1 Bashford Point",
"phone": "9745288064",
"join_date": "2008-09-18",
"status": "fulltime"
},{
"IDEmployee": "EMP4570787",
"IDBranch": "ALMST",
"IDJob": "PRG",
"name": "Mikey Ivanyushin",
"street": "1 Bashford Point",
"phone": "9745288064",
"join_date": "2008-09-18",
"status": "fulltime"
}]
and also I have 2 Struct like this
type EmployeeRequest struct {
IDEmployee string `json: "id_employee"`
IDBranch string `json: "id_branch" `
IDJob string `json: "id_job" `
Name string `json: "name" `
Street string `json: "street" `
Phone string `json: "phone" `
JoinDate string `json: "join_date" `
Status string `json: "status" `
Enabled int `json: "enabled" `
Deleted int `json: "deletedstring"`
}
type ListEmployeeRequest struct {
ListEmployee []EmployeeRequest `json: "request"`
}
for some reason the problem start here , when I try to run my function to read the json that I send from Postman
here is the function that I try to run
func insertEmployee(ctx context.Context) {
tempReq := services.ListEmployeeRequest{}
temp1 := ctx.ReadJSON(&tempReq.ListEmployee)
var err error
if err = ctx.ReadJSON(temp1); config.FancyHandleError(err) {
fmt.Println("err readjson ", err.Error())
}
parsing, errParsing := json.Marshal(temp1)
fmt.Println("", string(parsing))
fmt.Print("", errParsing)
}
the problem occur on the line if err = ctx.ReadJSON(temp1); config.FancyHandleError(err) it tell me that I got unexpected end of JSON input am I doing something wrong there when I throw the request from Postman? or I made a wrong validation code?
can someone tell me what to do next for me to be able to read the JSON that I throw from postman?
thanks for your attention and help , much love <3
You have multiple issues here:
id_employee
vs IDEmployee
.go vet
, it should be json:"id_employee"
You don't need another List
struct, if you use it your json should be {"requests":[...]}
. Instead you can deserialize a slice:
var requests []EmployeeRequest
if err := json.Unmarshal([]byte(j), &requests); err != nil {
log.Fatal(err)
}
You can see a running version here: https://play.golang.org/p/HuycpNxE6k8
type EmployeeRequest struct {
IDEmployee string `json:"IDEmployee"`
IDBranch string `json:"IDBranch"`
IDJob string `json:"IDJob"`
Name string `json:"name"`
Street string `json:"street"`
Phone string `json:"phone"`
JoinDate string `json:"join_date"`
Status string `json:"status"`
Enabled int `json:"enabled"`
Deleted int `json:"deletedstring"`
}
some of json tags didn't match with fields.