From b6ade99c2cb2fc3e764d70f87cf8b498bdb8ec78 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Thu, 30 May 2024 09:12:20 +0800 Subject: [PATCH] Fix handling of HTTP 304 (#159) https://github.com/com-lihaoyi/requests-scala/issues/105 304 is not a redirect, so it does not have a `location` header like other 3xx return codes, and is not a failure Added a unit test to confirm the fix --- requests/src/requests/Requester.scala | 6 ++++-- requests/test/src/requests/RequestTests.scala | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/requests/src/requests/Requester.scala b/requests/src/requests/Requester.scala index 0534768..974c7aa 100644 --- a/requests/src/requests/Requester.scala +++ b/requests/src/requests/Requester.scala @@ -291,7 +291,9 @@ case class Requester(verb: String, } } - if (responseCode.toString.startsWith("3") && maxRedirects > 0){ + if (responseCode.toString.startsWith("3") && + responseCode.toString != "304" && + maxRedirects > 0){ val out = new ByteArrayOutputStream() Util.transferTo(connection.getInputStream, out) val bytes = out.toByteArray @@ -344,7 +346,7 @@ case class Requester(verb: String, } } - if (streamHeaders.is2xx || !check) processWrappedStream(f) + if (streamHeaders.statusCode == 304 || streamHeaders.is2xx || !check) processWrappedStream(f) else { val errorOutput = new ByteArrayOutputStream() processWrappedStream(geny.Internal.transfer(_, errorOutput)) diff --git a/requests/test/src/requests/RequestTests.scala b/requests/test/src/requests/RequestTests.scala index 67af831..e0780dd 100644 --- a/requests/test/src/requests/RequestTests.scala +++ b/requests/test/src/requests/RequestTests.scala @@ -125,6 +125,10 @@ object RequestTests extends TestSuite{ } } + test("test_reproduction"){ + requests.get("http://httpbin.org/status/304").text() + + } test("streaming"){ val res1 = requests.get("http://httpbin.org/stream/5").text() assert(res1.linesIterator.length == 5)