douping5226 2019-08-12 11:04
浏览 112
已采纳

如何访问http客户端响应正文全局[关闭]

In the code below, am trying to access the variable "Regprofile" as global variable but and getting empty output. Any help?

type GMLCInstance struct {
    NfInstanceID   string   `json:"nfInstanceID"`
    HeartBeatTimer int      `json:"heartBeatTimer"`
    NfType         []string `json:"nfType"`
    NfStatus       []string `json:"nfStatus"`
    Ipv4Addresses  []string `json:"ipv4Addresses"`
}


var Regprofile GMLCInstance


// Request credentials and token from NRF and register profile to NFR database
func init() {

    urlcred := "https://127.0.0.1:9090/credentials"

    // Perform the request
    resp, err := netClient.Get(urlcred)
    if err != nil {
        log.Fatalf("Failed get: %s", err)
    }
    defer resp.Body.Close()

    // Fill the record with the data from the JSON
    var cr Credential

    // Use json.Decode for reading streams of JSON data
    if err := json.NewDecoder(resp.Body).Decode(&cr); err != nil {
        log.Println(err)
    }

    //fmt.Println(cr)

    clientId := cr.CLIENTID
    clientsec := cr.CLIENTSECRET

    // Get token

    reqtoken := url.Values{
        "grant_type":    []string{"client_credentials"},
        "client_id":     []string{clientId},
        "client_secret": []string{clientsec},
        "scope":         []string{"GMLC"},
    }

    urlq := "https://127.0.0.1:9090/oauth2/token?"

    res, err := netClient.PostForm(urlq+reqtoken.Encode(), nil)
    if err != nil {
        log.Fatalf("Failed get: %s", err)
    }


    var auth AccessToken

    // Use json.Decode for reading streams of JSON data
    if err := json.NewDecoder(res.Body).Decode(&auth); err != nil {
        log.Println(err)
    }

    //fmt.Println(auth.AccessToken)

    token := auth.AccessToken

    para := url.Values{
        "access_token": []string{token},
    }

    //============================================================
    // Register GMLC Instance to NRF, PUT

    var gmlc = []byte(`{
                "nfInstanceID": "f81d4fae-7dec-11d0-a765-00a0c91egmlc",
                "heartBeatTimer": 0,
                "nfType": ["GMLC"],
                "nfStatus": ["string"],
                "ipv4Addresses": ["172.16.0.X:5000"]
               }`)

    //================= Registser profile to NRF ========================//
    postnrf := "https://127.0.0.1:9090/nnrf-nfm/v1/nf-instances/f81d4fae-7dec-11d0-a765-00a0c91egmlc?"
    rq, err := http.NewRequest("PUT", postnrf+para.Encode(), bytes.NewBuffer(gmlc))
    rq.Header.Set("Content-Type", "application/json")
    rq.Header.Set("Accept", "application/json")

    response, err := netClient.Do(rq)
    if err != nil {
        panic(err)
    }
    defer res.Body.Close()

    decoder := json.NewDecoder(response.Body)

    var Regprofile GMLCInstance

    err = decoder.Decode(&Regprofile)
    if err != nil {
        fmt.Println("Response body not well decoded")
    }

    fmt.Println(Regprofile)

}

The output of fmt.Println(Regprofile) gives {f81d4fae-7dec-11d0-a765-00a0c91egmlc 10 [GMLC] [REGISTERED] [172.16.0.X:5000]}

However, when i print the variable in for example main as

func main(){

fmt.Println(Regprofile)

}

I get empty data as { 0 [] [] []}

  • 写回答

1条回答 默认 最新

  • dongyuji7309 2019-08-12 11:26
    关注

    In func init() you redeclare variables var Regprofile GMLCInstance locally to function scope. This declaration shadows global variable with local one. Just delete this local declaration inside init().

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

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试