You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2025/08/26 10:15:24 UTC

(camel) 01/01: CAMEL-22373: camel-http - Headers may be lost when calling HTTP service fails and exception is thrown

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch ht
in repository https://gitbox.apache.org/repos/asf/camel.git

commit c94f9cd66a707617d53414731f8961a4458d2894
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Aug 26 12:14:57 2025 +0200

    CAMEL-22373: camel-http - Headers may be lost when calling HTTP service fails and exception is thrown
---
 .../java/org/apache/camel/component/http/HttpProducer.java     | 10 +++++++---
 .../camel/component/http/HttpThrowExceptionOnFailureTest.java  |  3 +++
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
index 672eb853f7e..dcb72fc2521 100644
--- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
+++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
@@ -261,8 +261,6 @@ public class HttpProducer extends DefaultProducer implements LineNumberAware {
                             if (LOG.isDebugEnabled()) {
                                 LOG.debug("Http responseCode: {}", responseCode);
                             }
-                            populateResponseCode(exchange.getOut(), httpResponse, responseCode);
-
                             if (!throwException) {
                                 // if we do not use failed exception then populate response for all response codes
                                 HttpProducer.this.populateResponse(exchange, httpRequest, httpResponse, strategy, responseCode);
@@ -279,6 +277,9 @@ public class HttpProducer extends DefaultProducer implements LineNumberAware {
                                     HttpProducer.this.populateResponse(exchange, httpRequest, httpResponse, strategy,
                                             responseCode);
                                 } else {
+                                    // also store response code when throwing exception
+                                    populateResponseCode(exchange.getMessage(), httpResponse, responseCode);
+
                                     // operation failed so populate exception to throw
                                     throw HttpProducer.this.populateHttpOperationFailedException(exchange, httpRequest,
                                             httpResponse, responseCode);
@@ -354,9 +355,12 @@ public class HttpProducer extends DefaultProducer implements LineNumberAware {
             Exchange exchange, HttpUriRequest httpRequest, ClassicHttpResponse httpResponse,
             HeaderFilterStrategy strategy, int responseCode)
             throws IOException, ClassNotFoundException {
+
+        Message answer = exchange.getOut();
+        populateResponseCode(answer, httpResponse, responseCode);
+
         // We just make the out message is not create when extractResponseBody throws exception
         Object response = extractResponseBody(httpResponse, exchange, getEndpoint().isIgnoreResponseBody());
-        Message answer = exchange.getOut();
         answer.setBody(response);
 
         if (!getEndpoint().isSkipResponseHeaders()) {
diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpThrowExceptionOnFailureTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpThrowExceptionOnFailureTest.java
index 71a409a4fe1..c030d515a7f 100644
--- a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpThrowExceptionOnFailureTest.java
+++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpThrowExceptionOnFailureTest.java
@@ -76,6 +76,7 @@ public class HttpThrowExceptionOnFailureTest extends BaseHttpTest {
     @Test
     public void httpGetWhichReturnsHttp501ShouldThrowAnException() {
         Exchange reply = template.request(baseUrl + "/XXX?throwExceptionOnFailure=true", exchange -> {
+            exchange.getMessage().setHeader("cheese", "gauda");
         });
 
         Exception e = reply.getException();
@@ -88,6 +89,8 @@ public class HttpThrowExceptionOnFailureTest extends BaseHttpTest {
         Map<String, Object> headers = out.getHeaders();
         assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, headers.get(Exchange.HTTP_RESPONSE_CODE));
         assertEquals("Not Implemented", headers.get(Exchange.HTTP_RESPONSE_TEXT));
+        // header should be preserved
+        assertEquals("gauda", headers.get("cheese"));
     }
 
     @Test