English 中文(简体)
缩略语
原标题:POST a list of items using REST
  • 时间:2011-11-19 14:47:57
  •  标签:
  • rest
  • post

我期望在我有一份(很长的)清单,列出我想要向服务器投放的投稿时,就如何将我的数据序列化达成一项公约。

例如,如果我有资源<代码>/用户,而且我想有一份新的数据,那么,I d http-encode the areas for the newuser and put it in the request body such as this:name=foo&age=20

但是,如果我有一份用户名单,如本<代码>,[名称: foo , age: 20 },{名称: 条码,年龄: 10 },是否存在着一种常规的操作方法?

I m thinking name[0]=foo&age[0]=20&name[1]=bar&age[1]=10 but I can t find anything to back it up. What do web servers usually accept/expect?

最佳回答

可改变我的回答的快速问题: 您是不是直接来自HTML形式,或是期望某种更复杂(例如,javascript processsing,或甚至不是网络客户)

如果您的客户非常复杂,你就只能建造一个有内容的“密码”的“JSON”和“POST”系统。 然后,无论什么资源正在加工,都可以利用任何json图书馆阅读所张贴的插图和工艺。

Further Rambling:

你们用什么框架/语言来建设自己的教育、科学和技术服务? 他们是否有帮助你的内在功能/发明?

例如,如果你重新利用日本宇宙航空研究开发机构-RS来建设你的服务,就在通知中建造了一个装置: rel=“noreferer”>@FormParam,可以用来处理表格,例如:如果你在内容类别<编码>/x-www-form-urcoded <>/code>:=foo&=20¶ge=ampage。

可通过下列途径检索服务方面的平行名单:

@POST
@Consumes("application/x-www-form-urlencoded")
public void createUsers(@FormParam("name") List<String> name, @FormParam("age") List<String> age) {
    // Store your users
}

But you would then have to deal with the question of what if one list is shorter/longer than the other, how do you resolve that? What happens if a new field is required or optional to create a list of users? (But as I mentioned initially, a JSON array of JSON objects would solve that issue... there are a number of libraries out there that support automagic JSON deserialization in JAX-RS or there is also the option of creating your own MessageBodyReader.

(下一节中的争端: 我不了解铁路,我的经验在 Java服务界更多。 http://guides.rubyonrails.org/action_ Controller_overview.html” rel=“noreferer”>。 如同铁路公司一样,它有以下公约:<条码>,名称[]=foo&名称=bar,用于自动处理阵列中的数据,以及类似人口结构的公约,如<条码>用户[名称]=foo&用户[年龄]=20。 或许,如果你在铁路上,就能够以某种方式利用/利用这两种特征来取得预期的结果?

其他教育、科学和技术框架和语言可能具有自己的公约和功能:

问题回答

Rails serializes forms on a format not unlike what you suggest. If you have a nested model it encodes it like this:

name=theo&company[name]=acme

(对应的JSON应为{”名称:“theo”、“company”:{“姓名”:“acme >/code>)

I can t say that I ve seen a Rails application sending arrays, but there s no reason why it wouldn t work (worst case you would end up with a hash with string keys).

如果你想寄出一阵列,植物检疫局就拥有另一个公约。

names[]=alice&names[]=bob&names[]=steve

But I don t know how you do nested objects that way.

The HTTP spec, or if it su the URI spec, not ensuring which atm, actual specified that if You adopt the same debate over time You have range of Value (inab of the finalwins conduct of most application framework). 如:

但是,其中大多数适用于<代码>GET申请,不一定是POST。 (但application/x-url-encoded 应遵守<代码>GET相同的标准。

In short, I don t think there is a standard for doing this, POST bodies are a bit of a wild west territory. I think, however, that either you should go with JSON, because it s made to describe structures, and application/x-url-encoded is not, or you should try to represent the structure of your data better, something like:

users[0][name]=foo&users[0][age]=20&users[1][name]=bar&users[1][age]=10

这有某种机会,实际上可以由避风箱的铁路解释。





相关问题
Allow RESTful DELETE method in asp.net mvc?

im currently setting up asp.net to accept DELETE http verb in the application. However, when i send "DELETE /posts/delete/1" i always get a 405 Method not allow error. I tried to take a look at ...

Most appropriate API for URL shortening service

I ve just finished an online service for shortening URLs (in php5 with Zend Framework); you can enter an URL and you get an short URL (like tinyurl and such sites). I m thinking about the API for ...

Use HTTPClient or HttpUrlConnection? [closed]

We re implementing a REST client on JRE 1.4. Seems two good options for a client REST framework are HttpClient and HttpUrlConnection. Is there a reason to use HttpClient over the JRE s ...

Why can t I find the truststore for an SSL handshake?

I m using the Spring RESTTemplate on the client side to make calls to a REST endpoint. The client in this case is a Spring app and Tomcat is the servlet container. I m running into issues making a ...

Which Http redirects status code to use?

friendfeed.com uses 302. bit.ly uses 301. I had decided to use 303. Do they behave differently in terms of support by browsers ?

Three Step Buyonline The RESTful way

We are re-developing our buyonline functionality and we are doing it the RESTful way. The process is a three step one and the customer is asked to enter data at each step. Let s say the three URL s ...

热门标签