[ https://issues.apache.org/activemq/browse/CAMEL-1806?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=59078#action_59078 ]
Jeremy Norris commented on CAMEL-1806: -------------------------------------- In order to preserve the message body during "application/x-www-form-urlencoded" POSTs (for the purpose of proxying http endpoints), I've come up the following solution: This still propagates the form params to the message headers, but the body is preserved. Feedback is appreciated. Thanks. public class HttpBindingPreservePostFormUrlEncodedBody extends DefaultHttpBinding { private static final Logger LOG = Logger.getLogger(HttpBindingPreservePostFormUrlEncodedBody.class.getName()); public HttpBindingPreservePostFormUrlEncodedBody() { } public HttpBindingPreservePostFormUrlEncodedBody(HeaderFilterStrategy headerFilterStrategy) { super(headerFilterStrategy); } @Override public void readRequest(HttpServletRequest request, HttpMessage message) { // Read body first because DefaultHttpBinding delegate will look for // POST MediaType.APPLICATION_FORM_URLENCODED form params and remove // them from the body. It will then be our responsibility to add these // form params into the message headers to retain compatibility with // DefaultHttpBinding. InputStream inputStream = (InputStream) message.getBody(); String body = ""; try { body = IOUtils.toString(inputStream, "UTF-8"); } catch (IOException e) { throw new RuntimeException(e); } super.readRequest(request, message); message.setBody(body); String contentType = request.getContentType(); if (contentType != null && contentType.equals(MediaType.APPLICATION_FORM_URLENCODED)) { // Push POST form params into the headers to retain compatibility with DefaultHttpBinding try { for (String param : body.split("&")) { String[] pair = param.split("=", 2); String key = URLDecoder.decode(pair[0], "UTF-8"); String value = URLDecoder.decode(pair[1], "UTF-8"); message.getHeaders().put(key, value); } } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } } } And a unit test: public class TestHttpBindingPreservePostFormUrlEncodedBody extends CamelTestSupport { private static final String TEST_POST_BODY = "b1=x&b2=y"; @Test public void testSendPost() throws Exception { // The test will include POST params and query string params on the same // test to ensure everything ends up in the headers. // It will also ensure that the POST body is preserved when they are // form params. Client client = Client.create(); WebResource webResource = client.resource("http://localhost:9080/test?q1=x&q2=y"); WebResource.Builder requestBuilder = webResource.getRequestBuilder(); requestBuilder.type(MediaType.APPLICATION_FORM_URLENCODED); requestBuilder.post(String.class, TEST_POST_BODY); } protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() throws Exception { from("jetty://http://localhost:9080/test?httpBindingRef=httpBindingPreservePostFormUrlEncodedBody").process(new Processor() { public void process(Exchange exchange) throws Exception { Message in = exchange.getIn(); Assert.assertEquals("x", in.getHeader("q1")); Assert.assertEquals("y", in.getHeader("q2")); Assert.assertEquals("x", in.getHeader("b1")); Assert.assertEquals("y", in.getHeader("b2")); Assert.assertEquals(TEST_POST_BODY, in.getBody(String.class)); } }); } }; } @Override protected JndiRegistry createRegistry() throws Exception { JndiRegistry jndi = super.createRegistry(); jndi.bind("httpBindingPreservePostFormUrlEncodedBody", new HttpBindingPreservePostFormUrlEncodedBody()); return jndi; } } > put the query parameters into the message header > ------------------------------------------------ > > Key: CAMEL-1806 > URL: https://issues.apache.org/activemq/browse/CAMEL-1806 > Project: Apache Camel > Issue Type: Improvement > Components: camel-http > Reporter: Willem Jiang > Assignee: Willem Jiang > Fix For: 1.6.2, 2.0.0 > > > We will have the query parameter with POST and GET method. > But current Camel just support to put these parameters into the message > header with the http GET method. > Here is the user requirement of this[1], we should add this support in Camel. > [1] http://www.nabble.com/Jetty-component-and-HTTP-parameter-to24287116.html -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online.