Receiving 304 code after query from DB golang

435 views Asked by At

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

There are 2 answers

0
jgulacsy On

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

1
dm03514 On

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.