Ankur Choudhury
Ankur Choudhury

Reputation: 11

Issue with Apache Camel https rest api with username and password

I have the following piece of code, I've built for connecting to a "https" REST end point using Apache Camel. The problem is that I get 401 error if this is run.

from("timer:learnTimer?period=100s")
            .to("log:?level=INFO&showBody=true")
            .setHeader("currentTime", simple(currentTime))
            .setHeader(Exchange.CONTENT_TYPE,constant("application/json"))
            .setHeader(Exchange.HTTP_METHOD, constant("GET"))
            .setHeader(Exchange.HTTP_URI, simple("https://xxxxxx/api/siem/offenses?filter=status%20%3D%20%22OPEN%22%20and%20start_time%20%3E%201543647979000?&authMethod=Basic&authUsername=xxxxx&authPassword=xxxxx"))
            .to("https://xxxxxxx/api/siem/offenses?filter=status%20%3D%20%22OPEN%22%20and%20start_time%20%3E%201543647979000?&authMethod=Basic&authUsername=xxxx&authPassword=xxxx").convertBodyTo(String.class)
            .to("log:?level=INFO&showBody=true");

The error I am receiving is:

Stacktrace

org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking https://xx.xx.xx.xx/api/siem/offenses?filter=status+%3D+%22OPEN%22+and+start_time+%3E+1543647979000%3F with statusCode: 401 at org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:243) at org.apache.camel.component.http.HttpProducer.process(HttpProducer.java:165) at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61) at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148) at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548) at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) at org.apache.camel.processor.Pipeline.process(Pipeline.java:138) at org.apache.camel.processor.Pipeline.process(Pipeline.java:101) at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) at org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:197) at org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:79) at java.util.TimerThread.mainLoop(Timer.java:555) at java.util.TimerThread.run(Timer.java:505) 15:16| WARN | CamelLogger.java 213 | Error processing exchange. Exchange[ID-zabbixproxy-node2-1544019394005-0-1]. Caused by: [org.apache.camel.http.common.HttpOperationFailedException - HTTP operation failed invoking https://xx.xx.xx.xx/api/siem/offenses?filter=status+%3D+%22OPEN%22+and+start_time+%3E+1543647979000%3F with statusCode: 401] org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking https://10.96.40.66/api/siem/offenses?filter=status+%3D+%22OPEN%22+and+start_time+%3E+1543647979000%3F with statusCode: 401 at org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:243) at org.apache.camel.component.http.HttpProducer.process(HttpProducer.java:165) at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61) at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148)

Upvotes: 0

Views: 1833

Answers (1)

Amrut Malaji
Amrut Malaji

Reputation: 21

Are you sure you should set these header before making an rest call? un necessary request headers in IN Message may cause some issue.

 Exchange exchange = ExchangeBuilder.anExchange(camelContext)
                         .withHeader("").withProperty("")
                         .withPattern(ExchangePattern...)
                         .withHeader(Exchange.HTTP_METHOD, HttpMethod.GET)
                          .build();
            producer.send("the end point to rest",exchange); 

// producer is ProducerTemaplte

In above code you can set The ExchangePattern and required Headers and property (if only needed).

Hope this helps.

Upvotes: 0

Related Questions