How to dump HTTP body and headers sent with HTTP component with Apache Camel

匿名 (未验证) 提交于 2019-12-03 01:22:02

问题:

How to dump HTTP body and headers sent with Apache Camel HTTP component using this route:

   from('direct:abc').    setHeader(Exchange.HTTP_URI, constant("${config.gnutch.solr.coreUrl}/select")).    setHeader(Exchange.HTTP_QUERY, constant("q=${q}&wt=xml")).    setHeader(Exchange.CONTENT_TYPE, constant('application/xml')).    setHeader(Exchange.HTTP_METHOD, constant('GET')).    setBody(constant(null)).    to("http://null") 

This is Camel DSL code in groovy. Is that possible?

回答1:

Have you tried something like

from("direct:abc")  .to("http://domain.com/")  .to("log:DEBUG?showBody=true&showHeaders=true") 

Also the HTTP Component Documentation suggests that you can extract the HttpServletRequest from the exchange like,

HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class); 

You can then alternatively do,

from("direct:abc").to("http://domain.com").process(new Processor() {     public void process(Exchange exchange) throws Exception {         HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);         // Log request parameters     } }); 


回答2:

Try logging headers and content using one of HttpClient loggers.

Is is described in Logging Practices (version 3.x in this case).

I am using loggers with names:

  • httpclient.wire
  • org.apache.commons.httpclient.HttpConnection

which gives me output like:

o.a.c.httpclient.HttpConnection - Open connection to 0.0.0.0:12454 httpclient.wire.header - >> "POST /some/path HTTP/1.1[\r][\n]" httpclient.wire.header - >> "breadcrumbId: ID-localhost-55077[\r][\n]" httpclient.wire.header - >> "path: http://0.0.0.0:65432/some/other/path[\r][\n]" httpclient.wire.header - >> "User-Agent: Jakarta Commons-HttpClient/3.1[\r][\n]" httpclient.wire.header - >> "Host: 0.0.0.0:12454[\r][\n]" httpclient.wire.header - >> "Content-Length: 117[\r][\n]" httpclient.wire.header - >> "[\r][\n]" httpclient.wire.content - >> "{"a":"afeaafe","b":{"c":"53413"},"d":{"e":"vsegefawawewr"}}" httpclient.wire.header - << "HTTP/1.1 200 OK[\r][\n]" httpclient.wire.header - << "HTTP/1.1 200 OK[\r][\n]" httpclient.wire.header - << "Date: Fri, 08 Apr 2016 07:24:24 GMT[\r][\n]" httpclient.wire.header - << "Content-Type: application/octet-stream[\r][\n]" httpclient.wire.header - << "Date: Fri, 08 Apr 2016 07:24:24 GMT[\r][\n]" httpclient.wire.header - << "Content-Length: 7[\r][\n]" httpclient.wire.header - << "Server: Jetty(9.2.10.v20150310)[\r][\n]" httpclient.wire.header - << "[\r][\n]" httpclient.wire.content - << "Success" o.a.c.httpclient.HttpConnection - Releasing connection back to connection manager. 


回答3:

This will help , use this in log message :

${headers}

Or

${in.headers}

This will print any incoming headers .

Checkout here : http://camel.apache.org/simple.html

Example :



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!