douping5226
douping5226
2019-08-12 11:04

如何访问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 dongyuji7309 2年前

    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().

    点赞 评论 复制链接分享

相关推荐