繁体   English   中英

Apache骆驼如何以“body”格式提交body

[英]Apache camel how to submit body in “body” format

使用 Apache 骆驼,我有 Rest 组件。 看起来像:

    <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>

这个端点处理这样的精细请求:

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

(我可以看到它进入postRedirectProcessor ,这很好)。 但这不是我想要的。 我希望它处理这样的请求:

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

我的意思是,“数据”的格式不像k=v&k2=v2 ,而只是字符串,就像示例中一样(例如--data "something" )。

它会导致异常,它不会 go 进入 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]

我想,在 xml 中发布的param type="body"可以解决问题,但没有运气。

Curl 默认在--data中使用Content-Type: application/x-www-form-urlencoded header 发送数据。 请参阅如何使用 curl 发布原始身体数据?

x-www-form-urlencoded必须是键/值格式(规范)。 这就是为什么抛出异常的原因。

名称由=与值分隔,名称/值对由&分隔。

要发送原始数据,您需要指定另一个Content-Type

curl -v -i -H "Content-Type: text/plain" --data "hereisbody" http://localhost:8080/body

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM