On Thu, 17 Jan 2019, at 8:13 AM, aniruddha.dwiv...@nytimes.com wrote: > Hi I am using NYT's Marvin framework which internally uses gorilla mux. I am > able to delete single record when I pass single id as json object in request > body using postman but I don't know how to handle array of json objects here. > My code is as below:- > > For decoding --- > > func DecodeDeleteUser(ctx context.Context, r *http.Request) (interface{}, > error) { > /// uriDeleteRequest := &UserDel{} > uriDeleteRequest := &[]UserDel{} > log.Infof(ctx, "DeleteUser before decoding :::::: %+v", r) > if err := json.NewDecoder(r.Body).Decode(uriDeleteRequest); err != nil { > log.Errorf(ctx, "Delete User Error : Could not decode body from the request") > return nil, marvin.NewJSONStatusResponse( > "could not decode request body in DeleteUser", > http.StatusBadRequest, > ) > } > log.Infof(ctx, "DeleteUser After decoding ::: decoding done successfully", > uriDeleteRequest) > return uriDeleteRequest, nil > } > > It successfully decodes array of JSON. Now problem arises is function below- > > func(s service) deleteUserFromDb(ctx context.Context, request interface{}) > (interface{}, error) { > // var userdel []UserDel > log.Infof(ctx, "request just inside method deleteUserFromDb : %+V",request) > r := request.([]*UserDel) > log.Infof(ctx, "request in form of r is : %+V",r) > // db, err := s.ConnectsService.initPostgreConnection(ctx) > _, err := s.ConnectsService.initPostgreConnection(ctx) > if err != nil { > log.Infof(ctx, "Connection established in deleteUserFromDb method ...") > return nil,err > } > return "all fine",nil > } > > Here line * r := request.([]*UserDel) *gives following error - panic: > interface conversion: interface {} is *[]test_marvin.UserDel, not > []*test_marvin.UserDel > > Here test_marvin is my code's package name. Please help as I don't know what > is wrong with my above code in golang? >
In DecodeDeleteUser it looks like your request is a pointer to a slice of UserDel but in deleteUserFromDb you are expecting it to be a slice of pointers to UserDel. Change uriDeleteRequest := &[]UserDel{} to be uriDeleteRequest := []*UserDel{} and it should work. All the best, Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.