1

I am using Apache Camel to expose an internal REST endpoint. The internal endpoint returns code 400 (BAD REQUEST), alongside with useful JSON with error details.

By default, Camel reacts by returning code 500. How can I get Camel to pass on code 400 with its accompanying JSON body?

Camel route:

    rest("/internal")
            .post("/getServiceState")
            .to("direct:getServiceState");

    from("direct:getServiceState")
            .to("http://endpoint/getServiceState" + "?bridgeEndpoint=true");
tonysepia
  • 3,340
  • 3
  • 27
  • 45
  • Does this answer your question? [Apache Camel: Unable to get the Exception Body](https://stackoverflow.com/questions/41008023/apache-camel-unable-to-get-the-exception-body) – lcnicolau Jan 20 '22 at 19:59

1 Answers1

4

With the okStatusCodeRange parameter, you can develop as if the application did not receive an error.

.to("http://endpoint/getServiceState" + "?bridgeEndpoint=true&okStatusCodeRange=200-299,400-501").process(e-> {// do something })

also , You can use cbr to take action according to the status of the incoming status code.

.choice().when().simple("${header.CamelHttpResponseCode} == '400'")
            .unmarshal().json(MigrosErrorResponse.class).otherwise().process(e-> {})
erayerdem
  • 775
  • 6
  • 12