Camel REST to external API not returning the response

801 views Asked by At

I'm using camel-rest to connect to external API and validate json-schemas, the app works as intended but, there's no response coming back on calls, I can only see it through logs inside the Camel as shown below:

INFO  [route3] (vert.x-worker-thread-0) HTTP Response: [{"description":"Winter fruit","name":"Apple"},{"description":"Tropical fruit","name":"Pineapple"}]

And any curl made only returns 200 OK.

This is my code:

public class Validator extends RouteBuilder {

    private static final String REST_ENDPOINT_ECHO= "{{rest.endpoint}}?bridgeEndpoint=true" +
            "&copyHeaders=true" +
            "&connectionClose=true";

    @Override
    public void configure() throws Exception {
        onException(JsonValidationException.class)
            .handled(true)
            .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(400))
            .setHeader(Exchange.CONTENT_TYPE, simple("text/plain"))
            .log("Body: ${body}, Header: ${headers}")
            .setBody(simple("Error: Invalid JSON Schema"));

        rest()
            .post("/{{rest.endpoint.subpath}}").enableCORS(true).route()
            .to("direct:validator")
            .endRest();

        from("direct:echoServiceUrl")
             .to("json-validator:file:/deployments/schema.json")
             .to("http://" + REST_ENDPOINT_ECHO)
                 .log("HTTP Response: " + "${body}")
                 .convertBodyTo(String.class)
        .end();
1

There are 1 answers

0
erayerdem On BEST ANSWER

Http component returns CachedOutputStream so when u log it This stream object is being consumed and u get an empty response . this way your stream object is stored in the next steps. first solution u can set to streamcaching to your code like that

.to("https://anyurl.com")
    .streamCaching()
    .log("my body is ${body}");

Other way u must delete log line

.log("HTTP Response: " + "${body}")