How to authorize using Apache Camel?
I have to make a POST request
curl -X POST --data-binary @auth.json http://somehost.com/auth
{
"response": {
"status": "OK",
"token": "622cee5f8c99c81e87614e9efc63eddb"
}
}
, and this will return a JSON response with the token. auth.json is a JSON file with login and p开发者_如何学Cassword. I then have two options: put the token in the header in future requests as "Authorization: TOKEN", or put it in a cookie and make other requests. How can I do it with Apache Camel? How can I receive HTTP response? Where do I put the token? Now I have:
public static void main(String args[]) throws Exception {
CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
public void configure() {
from("file:data/inbox?noop=true")
.to("http://somehost.com/auth");
}
});
context.start();
Thread.sleep(10000);
context.stop();
} and I have the auth.json file in ./data/inbox
Since you posted the same question on the Apache Camel mailing list I've provided an answer there.
To summarize: Just call setHeader("Authorization", constant("622cee5f8c99c81e87614e9efc63eddb"))
in your route before sending the http request. Camel will automatically translate this header to a transport specific (in this case HTTP) header. Of course you don't need to provide a constant token in your route, you can dynamically calculate or lookup the token by using a Camel expression or processor.
Your complete route will look something like:
context.addRoutes(new RouteBuilder() {
public void configure() {
from("file:data/out?fileName=filename.json&noop=true")
.setHeader("Authorization", constant("mytoken"))
.to("http://somehost.com/auth");
}
精彩评论