java 使用 xml 设置 Restful POST 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1663612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting up a Restful POST request using xml
提问by brock
I'm working on setting up a RESTful request for the application I'm working on and I wanted to use xml as the request in the uri instead of allowing the client to supply the parameters in the URI itself.
我正在为我正在处理的应用程序设置 RESTful 请求,我想使用 xml 作为 uri 中的请求,而不是允许客户端在 URI 本身中提供参数。
I'm looking to have the URI like so: someurl/service/request
我希望有这样的 URI:someurl/service/request
instead of: someurl/service/request?id={id}&name={name}
而不是: someurl/service/request?id={id}&name={name}
I have been searching the web to see what the convention should be when creating the POST request. Can anyone kind of help point me in the right direction on how I should set up this POST request allowing the client to use xml?
我一直在网上搜索以查看创建 POST 请求时的约定。任何人都可以帮助我指出我应该如何设置这个 POST 请求以允许客户端使用 xml 的正确方向吗?
Not sure if it is relevant but I'm setting up the server side code in JAVA using the SPRING 3.0 framework. Please let me know if I need to supply more details.
不确定它是否相关,但我正在使用 SPRING 3.0 框架在 JAVA 中设置服务器端代码。如果我需要提供更多详细信息,请告诉我。
Thanks for your help!!
谢谢你的帮助!!
采纳答案by Myles
You can put parameters into the body of the request. They are the same format as appending them to the URL. Eg:
您可以将参数放入请求正文中。它们与将它们附加到 URL 的格式相同。例如:
POST /path/script.cgi HTTP/1.0
User-Agent: HTTPTool/1.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 32
home=Cosby&favorite+flavor=flies
You can do that in prototype pretty easily with:
你可以很容易地在原型中做到这一点:
new Ajax.Request('someurl/service', {
method: 'post',
postBody: 'home=Cosby&favorite+flavor=flies',
encoding: 'UTF-8'});
To add your xml file, just append it to your postBody with some sort of delimiter so your cgi knows where parameters end and where xml begins.
要添加您的 xml 文件,只需使用某种分隔符将其附加到您的 postBody 中,以便您的 cgi 知道参数在哪里结束以及 xml 在哪里开始。
I think that's what you were looking for, hope it helps.
我想这就是你要找的,希望它有帮助。
回答by Marcus Leon
You can pass whatever you want in your POSTbody. So if you want to use XML, you can use XML. Example:
你可以通过你的POST身体任何你想要的。所以如果你想使用 XML,你可以使用 XML。例子:
POST /car
Content-Type: text/xml
<car>
<date>10-10-2007<date>
<type>Corvette</type>
</car>
HTTP/1.1 201 CREATED
I think all the REST API frameworks let you easily specify XML in the client request and server response. See Restlet's quick startfor an example.
我认为所有 REST API 框架都可以让您轻松地在客户端请求和服务器响应中指定 XML。有关示例,请参阅 Restlet 的快速入门。

