duanao6704 2016-12-24 22:53
浏览 72

从数据库golang查询后接收304代码

I am trying to implement an API at the moment, in the first step a session is created, and after this sesson is queried with a get request. Sometimes I get a response code of 304 Not Modified – the results have not been modified since the last poll, and it seems to be all random to me. In this case I am unable to save the response into a Golang structure. It is very frustrating. Do you have any insights what could be the problem? Thanks!

client := &http.Client{Timeout: 10 * time.Second}
req, err := http.NewRequest("GET", url, nil)
q := req.URL.Query()
q.Add("apiKey", apiKey)
q.Add("sortorder", "asc")
req.URL.RawQuery = q.Encode()

if err != nil {
    panic(err)
}
req.Header.Set("ACCEPT", "application/json")
resp, err := client.Do(req)
if err != nil {
    panic(err)
}

defer resp.Body.Close()
if resp.StatusCode == 200{
    if err != nil {
        panic(err)
    }
}else{
    fmt.Println(resp.StatusCode)
}
data, err := ioutil.ReadAll(resp.Body)

return []byte(data)
  • 写回答

2条回答 默认 最新

  • dongyan8896 2016-12-25 02:01
    关注

    Are implementing the server too? Or just a client? Do you control the server? If you don't then you'll probably have to handle 304 not modified. It could be nice if the server tells you the resource has not been modified, because you may be able to short circuit some processing.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用