dongnai5905 2019-02-13 08:14
浏览 35
已采纳

Golang Rest API数据未在邮递员中显示

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

  • 写回答

1条回答 默认 最新

  • dpjs2005 2019-02-13 08:23
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?