gpt4 book ai didi

go - 如何从另一个请求返回一个主体

转载 作者:行者123 更新时间:2023-12-01 22:43:06 27 4
gpt4 key购买 nike

req, err := http.NewRequest("GET", "https://api.github.com/repos/octocat/Hello-World/pulls/1347", nil)
req.Header.Set("Accept", "application/vnd.github.v3.patch")
if err != nil {
check(err)
}
body, err := ioutil.ReadAll(req.Body)
ctxt.JSON(http.StatusOK, body)

在这里,我需要从github api主体发送api响应。但是在这里,我得到以下错误:

“运行时错误:无效的内存地址或nil指针取消引用”

最佳答案

此代码将解决您的问题。

client := &http.Client{}
apiURL := "https://api.github.com/repos/octocat/Hello-World/pulls/1347"
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
check(err)
}
req.Header.Add("Accept", "application/vnd.github.v3.patch")
response, err := client.Do(req)
if err != nil {
check(err)
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
check(err)
}
ctxt.JSON(http.StatusOK, string(contents))

关于go - 如何从另一个请求返回一个主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59155157/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com