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

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


The following commit(s) were added to refs/heads/main by this push:
     new defa8fc9ef8 CAMEL-22373: camel-http - Headers may be lost when calling 
HTTP service fails and exception is thrown (#18999)
defa8fc9ef8 is described below

commit defa8fc9ef880c4a66ee1615e8329d827fc552a3
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Aug 26 12:39:04 2025 +0200

    CAMEL-22373: camel-http - Headers may be lost when calling HTTP service 
fails and exception is thrown (#18999)
---
 .../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

Reply via email to