0

Using Apache camel, I have Rest component. It looks like:

    <post uri="/body" method="POST">
        <description>Here is post method</description>
        <param name="save" type="body" dataType="string"/>
        <route>
            <process ref="postRedirectProcessor" />
            <to uri="direct:commonRoute" />
        </route>
    </post>

And this endpoint processes fine requests like this:

curl -i --data "b=hereisbody" http://localhost:8080/body (works fine, but I don't need it)

(I can see it goes into postRedirectProcessor and that is fine). But this is not what I want. I want it to process requests like this:

curl -i --data "hereisbody" http://localhost:8080/body (doesn't work, causes 405)

I mean, format of "data" is not like k=v&k2=v2, but it is just string, like in example (such as --data "something").

It causes an exception, it doesn't go into postRedirectProcessor.

2020-04-10 18:43:09,716 ERROR [http-nio-8080-exec-6] - ,,, - Servlet.service() for servlet [CamelServlet] in context with path [] threw exception
java.lang.IllegalArgumentException: Invalid parameter, expected to be a pair but was hereisbody
    at org.apache.camel.http.common.DefaultHttpBinding.readFormUrlEncodedBody(DefaultHttpBinding.java:272) ~[camel-http-common-2.24.3.jar:2.24.3]
    at org.apache.camel.http.common.DefaultHttpBinding.readRequest(DefaultHttpBinding.java:116) ~[camel-http-common-2.24.3.jar:2.24.3]
    at org.apache.camel.http.common.HttpMessage.<init>(HttpMessage.java:56) ~[camel-http-common-2.24.3.jar:2.24.3]
    at org.apache.camel.http.common.CamelServlet.doService(CamelServlet.java:187) ~[camel-http-common-2.24.3.jar:2.24.3]
    at org.apache.camel.http.common.CamelServlet.service(CamelServlet.java:79) ~[camel-http-common-2.24.3.jar:2.24.3]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.31.jar:9.0.31]

I thought, param type="body" as in xml posted, does the trick, but no luck.

Den Doeson
  • 168
  • 1
  • 13

1 Answers1

1

Curl sends data in --data with Content-Type: application/x-www-form-urlencoded header by default. See How to post raw body data with curl?

x-www-form-urlencoded must be in key/value format (specification). This is reason, why exception is thrown.

The name is separated from the value by = and name/value pairs are separated from each other by &.

To send raw data you need to specify another Content-Type.

curl -v -i -H "Content-Type: text/plain" --data "hereisbody" http://localhost:8080/body
Bedla
  • 4,789
  • 2
  • 13
  • 27
  • That's right. But what if I can not change input requests? Is there a way to process them somehow anyway? I mean, disable input validations or something... – Den Doeson Apr 13 '20 at 05:01
  • Finally realized that I CAN change format, so -H "Content-Type: text/plain" works fine) – Den Doeson Apr 14 '20 at 08:21