-2

I am facing an issue where i have made an api in Go every thing work fine but i am not getting data in postman. When i print the data in logs i am getting the data properly but it is showing blank data in postman.

authorizeModel.go

func GetSkillList() map[string]interface{} {
    db := GetDB()
    var (
        // id        int
        skillName string
    )
    type SkillList struct {
        name string
    }
    skillList := SkillList{}
    skillArr := []SkillList{}

    rows, err := db.Query("select DISTINCT(name) as name from skills where company_id IN ('2') and name != 'Skill Needed' order by name")
    if err != nil {
        log.Fatal(err)
    }
    defer rows.Close()
    for rows.Next() {
        err := rows.Scan(&skillName)
        if err != nil {
            log.Fatal(err)
        }
        skillList.name = skillName
        skillArr = append(skillArr, skillList)
    }
    response := u.Message(true, "Skill list retrieved successfully")
    response["data"] = skillArr
    log.Println(skillArr)
    response["authorization"] = false
    return response
}

authController.go

var SkillTagList = func(w http.ResponseWriter, r *http.Request) {

    resp := models.GetSkillList()
    u.Respond(w, resp)
}

routes.go

router.HandleFunc("/api/v1/authorize/skillTagList", controllers.SkillTagList).Methods("POST")

If you see authorizeModel.go i have printed my data in logs i am getting that data successfully in logs. But see the postman screenshot below.

enter image description here

Aadil Shaikh
  • 389
  • 1
  • 5
  • 22
  • 1
    You have to export fields if you want to marshal them. – Peter Feb 13 '19 at 08:23
  • 1
    Possible duplicate of [JSON and dealing with unexported fields](https://stackoverflow.com/questions/11126793/json-and-dealing-with-unexported-fields) – Peter Feb 13 '19 at 08:24

1 Answers1

2

You have to rename name to Name

I'm not sure what is u.Respond(), so I will assume it's a helper function of some framework that you are using, and I will assume u.Respond() is internally using json.Marshal.

If your struct has unexported fields(fields name starting with lowercase letter, in your case name), json.Marshal cannot access those field, and the result won't have name field. That is why you are getting empty objects in JSON.

Praveen
  • 2,400
  • 3
  • 23
  • 30