登录
首页 >  Golang >  Go问答

如何在http客户端中访问响应体

来源:stackoverflow

时间:2024-03-10 20:27:25 480浏览 收藏

目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《如何在http客户端中访问响应体》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~

问题内容

在下面的代码中,我尝试将变量“regprofile”作为全局变量访问,但得到空输出。有什么帮助吗?

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(®profile)
    if err != nil {
        fmt.println("response body not well decoded")
    }

    fmt.println(regprofile)

}

fmt.println(regprofile) 的输出给出 {f81d4fae-7dec-11d0-a765-00a0c91egmlc 10 [gmlc] [已注册] [172.16.0.x:5000]}

但是,当我在 main 中打印变量时

func main(){

fmt.Println(Regprofile)

}

我得到的空数据为 { 0[][][]}


解决方案


func init() 中,您可以在本地重新声明变量 var Regprofile GMLCInstance 到函数作用域。该声明将全局变量与局部变量隐藏起来。只需删除 init() 中的本地声明即可。

理论要掌握,实操不能落!以上关于《如何在http客户端中访问响应体》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>