Recently I noticed that when I did a http get to one of my API endpoints at work the request would come back with the same data no matter what. I knew the data should be coming back differently because I would make a change to the backend data and it was not be reflected in the response. To ensure that my API was working as expected I was able to verify that the response was coming back correctly in Postman as well as directly from the URL in Chrome.
I suspected my issue had something to do w/ request caching coming from the same client because I was making my get requests back to back, within 5 seconds. To test my theory I added both a ‘Cache-Control’ and ‘Pragma’ request header to my request and set them both to ‘no-cache’ but that did not help.
Being a bit stumped here I reached out to our API developers to enlist their help. The first thing they did was start watching the incoming traffic and noticed that my request was never actually leaving the device so there was no way it would ever return the new data. To ensure this was just an emulator issue I deployed to my actual phone to see if the issue persisted there and sure enough it did.
We now knew that something inside of the Windows Phone was doing Http Get request caching… but how to fix it.
We figured we could solve the problem the simple, non-elegant way which is to take on a random query string to each request thus forcing each request to be unique. This did work, but man did that feel dirty.
After doing a bit of research I came across the ‘If-Modified-Since’ request header. The specs on this state the following:
The If-Modified-Since request-header field is used with a method to make it conditional: if the requested variant has not been modified since the time specified in this field, an entity will not be returned from the server; instead, a 304 (not modified) response will be returned without any message-body.
This should work great all I needed to do was use this with the current date time. Sure enough I added this request header along with DateTime.Now() and my get request made it to the server and I was able to get my updated data as expected.
Hope this helps,
Till next time,
09-17-2013 5:00 AM