msimonborg
Possible hidden caching default in Req, receiving status 304
I thought about opening an issue for this question in the Req repo, but I think it’s more an issue with my HTTP knowledge. I’m hoping someone here can point me in the right direction.
I have an application that builds a database from an external data source. The data is available in .zip files and downloaded by performing get requests to known URLs. I am performing the requests like this
Req.get!(url, raw: true, output: file_path)
Occasionally I want to rebuild the database in development. Lately I’ve been getting exceptions during some attempts to rebuild when the files cannot be unzipped, due to invalid file contents {:error, :einval}. I traced the error to the request response, which is returning a status 304 "not modified" and a body "". The empty body contents are being written to the output file path, causing the unzip error downstream.
%Req.Response{
body: "",
headers: [
{"content-type", "application/zip"},
{"last-modified", "Wed, 22 Sep 2021 19:48:44 GMT"},
{"cache-control", "private, max-age=169126"},
{"date", "Sun, 03 Jul 2022 15:21:43 GMT"},
{"connection", "keep-alive"}
],
private: %{},
status: 304
}
Is there any way to get around the 304, or to force my way to a 200 with the desired response body? Or is there just no workaround as long as I’m requesting the same contents from the same IP within the server’s cool-down period?
Thanks in advance for any suggestions.
Marked As Solved
wojtekmach
OK, I was able to reproduce it with this script:
Mix.install([:req])
url = "https://www2.census.gov/geo/tiger/TIGER2021/SLDL/tl_2021_50_sldl.zip"
Enum.each(1..50, fn _ ->
IO.inspect(Req.get!(url).status)
Process.sleep(2000)
end)
interestingly, for me, it always either printer 200,200,… OR 304,304,… never one or the other (though I didn’t run it for long)
%Req.Response{headers: [..., {"vary", "Accept-Encoding"}, ...]}
Req by default sets accept-encoding with gzip and others. Maybe that is confusing the server? You can turn it off like this: compressed: false.
I have tried this a few times and always kept getting 200s:
Mix.install([:req])
url = "https://www2.census.gov/geo/tiger/TIGER2021/SLDL/tl_2021_50_sldl.zip"
Enum.each(1..50, fn _ ->
IO.inspect(Req.get!(url, compressed: false).status)
Process.sleep(2000)
end)







