I would like to make my application somewhat REST compliant. I am using Rails on the backend and GWT on the frontend. I would like to do updates and deletes. I realize I can do something like mydomain.com/:id/delete (GET) and accomplish the same thing. However, as I stated previously, I would like to have a REST compliant backend. Thus, I want to do mydomain.com/:id (DELETE) and have it implicitly call my delete method.
我想使我的应用程序有点REST兼容。我在后端使用Rails,在前端使用GWT。我想做更新和删除。我意识到我可以做一些像mydomain.com/:id/delete(GET)这样的事情并完成同样的事情。但是,正如我之前所说,我希望有一个REST兼容的后端。因此,我想做mydomain.com/:id(DELETE)并让它隐式调用我的删除方法。
Now, it's my understanding that if a browser (my browser is GWT RequestBuilder) doesn't support DELETE/GET, Rails somehow accomplishes this task with a POST and some other url parameter. So, how can I accomplish this with a GWT RequestBuilder?
现在,我的理解是,如果浏览器(我的浏览器是GWT RequestBuilder)不支持DELETE / GET,Rails会以某种方式使用POST和其他一些url参数完成此任务。那么,我如何通过GWT RequestBuilder实现这一目标?
2 个解决方案
#1
7
Rails does this with hidden attributes. The easiest way to figure this out would be to create a new rails application, generate a scaffold and have a look at the HTML in a browser.
Rails使用隐藏属性执行此操作。解决这个问题的最简单方法是创建一个新的rails应用程序,生成一个脚手架并在浏览器中查看HTML。
Try this:
尝试这个:
rails jp
cd jp
./script/generate scaffold RequestBuilder name:string
rake db:migrate
./script/server
Then navigate to http://localhost:3000/request_builders, click on New and have a look at the HTML. You'll see something like:
然后导航到http:// localhost:3000 / request_builders,单击New并查看HTML。你会看到类似的东西:
<form action="/request_builders" class="new_request_builder"
id="new_request_builder" method="post">
<div style="margin:0;padding:0">
<input name="authenticity_token" type="hidden" value="e76..." />
</div>
This is a creation, method is POST. Enter a name, save then Edit:
这是一个创建,方法是POST。输入名称,然后保存,然后编辑:
<form action="/request_builders/1" class="edit_request_builder"
id="edit_request_builder_1" method="post">
<div style="margin:0;padding:0">
<input name="_method" type="hidden" value="put" />
<input name="authenticity_token" type="hidden" value="e76..." />
</div>
Of course the form is sent with POST, but Rails hads a hidden field to simulate a PUT request. Same for deletion, but the scaffold will do it with a bit of Javascript:
当然,表单是通过POST发送的,但是Rails有一个隐藏字段来模拟PUT请求。同样的删除,但脚手架将使用一些Javascript:
var m = document.createElement('input');
m.setAttribute('type', 'hidden');
m.setAttribute('name', '_method');
m.setAttribute('value', 'delete');
To have this work with another front-end, you'll have to both:
要让这个与另一个前端一起工作,你必须同时:
- Use the same style URL such as /request_builders/1 (RESTful URLs)
- 使用相同的样式URL,例如/ request_builders / 1(RESTful URL)
- Include the hidden fields (Rails trick)
- 包括隐藏字段(Rails技巧)
#2
6
Like @skrat said, the _method=PUT
workaround doesn't work for any kind of body where Content-Type
is not x-www-form-urlencoded
, e.g. XML or JSON. Luckily, there is a header workaround as well:
就像@skrat所说的那样,_method = PUT解决方法对于Content-Type不是x-www-form-urlencoded的任何类型的主体都不起作用,例如: XML或JSON。幸运的是,还有一个标题解决方法:
https://zcox.wordpress.com/2009/06/17/override-the-http-request-method-in-jersey/
https://zcox.wordpress.com/2009/06/17/override-the-http-request-method-in-jersey/
So to update a REST resource, just do a POST to its address and add the header X-HTTP-Method-Override: PUT
. Rails will interpret this as a PUT to the address.
因此,要更新REST资源,只需对其地址执行POST并添加标头X-HTTP-Method-Override:PUT。 Rails会将此解释为地址的PUT。
#1
7
Rails does this with hidden attributes. The easiest way to figure this out would be to create a new rails application, generate a scaffold and have a look at the HTML in a browser.
Rails使用隐藏属性执行此操作。解决这个问题的最简单方法是创建一个新的rails应用程序,生成一个脚手架并在浏览器中查看HTML。
Try this:
尝试这个:
rails jp
cd jp
./script/generate scaffold RequestBuilder name:string
rake db:migrate
./script/server
Then navigate to http://localhost:3000/request_builders, click on New and have a look at the HTML. You'll see something like:
然后导航到http:// localhost:3000 / request_builders,单击New并查看HTML。你会看到类似的东西:
<form action="/request_builders" class="new_request_builder"
id="new_request_builder" method="post">
<div style="margin:0;padding:0">
<input name="authenticity_token" type="hidden" value="e76..." />
</div>
This is a creation, method is POST. Enter a name, save then Edit:
这是一个创建,方法是POST。输入名称,然后保存,然后编辑:
<form action="/request_builders/1" class="edit_request_builder"
id="edit_request_builder_1" method="post">
<div style="margin:0;padding:0">
<input name="_method" type="hidden" value="put" />
<input name="authenticity_token" type="hidden" value="e76..." />
</div>
Of course the form is sent with POST, but Rails hads a hidden field to simulate a PUT request. Same for deletion, but the scaffold will do it with a bit of Javascript:
当然,表单是通过POST发送的,但是Rails有一个隐藏字段来模拟PUT请求。同样的删除,但脚手架将使用一些Javascript:
var m = document.createElement('input');
m.setAttribute('type', 'hidden');
m.setAttribute('name', '_method');
m.setAttribute('value', 'delete');
To have this work with another front-end, you'll have to both:
要让这个与另一个前端一起工作,你必须同时:
- Use the same style URL such as /request_builders/1 (RESTful URLs)
- 使用相同的样式URL,例如/ request_builders / 1(RESTful URL)
- Include the hidden fields (Rails trick)
- 包括隐藏字段(Rails技巧)
#2
6
Like @skrat said, the _method=PUT
workaround doesn't work for any kind of body where Content-Type
is not x-www-form-urlencoded
, e.g. XML or JSON. Luckily, there is a header workaround as well:
就像@skrat所说的那样,_method = PUT解决方法对于Content-Type不是x-www-form-urlencoded的任何类型的主体都不起作用,例如: XML或JSON。幸运的是,还有一个标题解决方法:
https://zcox.wordpress.com/2009/06/17/override-the-http-request-method-in-jersey/
https://zcox.wordpress.com/2009/06/17/override-the-http-request-method-in-jersey/
So to update a REST resource, just do a POST to its address and add the header X-HTTP-Method-Override: PUT
. Rails will interpret this as a PUT to the address.
因此,要更新REST资源,只需对其地址执行POST并添加标头X-HTTP-Method-Override:PUT。 Rails会将此解释为地址的PUT。