0

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)
jgulacsy
  • 117
  • 9

2 Answers2

1

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.

dm03514
  • 54,664
  • 18
  • 108
  • 145
  • I am just implementing the client using a third party server. My problem is that if I receive the 304 I don't have access to the response body. The strange thing is, if I manually open the url of the response it takes me to a proper json structure. – jgulacsy Dec 25 '16 at 14:28
0

Okay, problem has been solved by waiting for 1 second and polling again.

jgulacsy
  • 117
  • 9