简体   繁体   English

带主体的角度$ http.delete请求

[英]Angular $http.delete request with body

So I looked at this post: is an entity body allowed for an http delete request 所以我看了这篇文章: 是否允许实体实体进行http删除请求

Which seems to indicate that while it is 'ok' to do on some conceptual level, in practice it may not be doable because browsers just ignore it. 这似乎表明,虽然可以在某种概念上做到这一点,但实际上它可能并不可行,因为浏览器只是忽略了它。

I have some express.js authentication middleware I need to get through, and I don't want to attach my user details to url params. 我需要通过一些express.js身份验证中间件,并且我不想将用户详细信息附加到url参数中。 All my other requests that need to authenticate attach these details to the body of the request. 我所有其他需要验证的请求都将这些详细信息附加到请求的正文中。

Is there some way to force this? 有什么办法可以强制这样做吗? I saw some other posts where some people seemed to have success in passing a body with their delete request. 我看到了其他一些帖子,其中有些人似乎成功地通过了删除请求传递了尸体。

I am running a node/sails back-end. 我正在运行节点/帆后端。 It always logs the body as undefined for a delete request. 对于删除请求,它始终将主体记录为未定义。 Is there any way to modify 有什么办法可以修改

The sails API pulls the id of the object to delete from the params, so we have to append the id to the url. Sails API提取要从参数中删除对象的ID,因此我们必须将ID附加到URL。

But if I want to pass some authentication details in a body for server-side verification before processing the delete request, I can't just stick them in an object as the second parameter of the delete request, like you can with $http.post. 但是,如果我想在处理删除请求之前在主体中传递一些身份验证详细信息以进行服务器端验证,则不能像在$ http.post中那样将它们作为删除请求的第二个参数粘贴到一个对象中。 。

Angular's post method automatically assigns whatever we insert as a second parameter to the body of the request, but the delete method does not . Angular的post方法会自动将我们作为第二个参数插入的内容分配给请求的主体,而delete方法则不会

Angular's $http.delete method does allow us to supply a config object as the second parameter, through which we can get access to the 'data' property. Angular的$ http.delete方法确实允许我们提供一个配置对象作为第二个参数,通过它我们可以访问'data'属性。 This is the same way post does it through it's second parameter. 这与post通过其第二个参数进行操作的方式相同。

So if we need to attach a body to a delete request we can use the following: 因此,如果需要将正文附加到删除请求,则可以使用以下内容:

$http.delete('/api/' + objectToDelete.id, {data: {id: currentUser().id, level: currentUser().level}});

This will pass the object to delete's id in the url parameter, and my user credentials in the body as an object. 这将在url参数中传递要删除的ID的对象,并在正文中将我的用户凭据作为对象传递。

Honestly, everytime a trouble sounds like a "restriction of as REST", a rethink of the strategy and the philosophy might be a good idea. 老实说,每当麻烦听起来像是“ REST的限制”时,重新考虑策略和理念可能是个好主意。

I have some authentication middleware I need to get through 我需要通过一些身份验证中间件

I don't want to attach my user details to url params 我不想将用户详细信息附加到url参数

I'm not directly answering the question, but you should know that among the commons 我没有直接回答这个问题,但是您应该知道

  • URL parameters (or query, but URL anyway) URL参数(或查询,但仍然是URL)
  • Body 身体

there is a third option for "passing values to the server" : 第三种选择是“将值传递到服务器”:

  • request Headers 请求标题

I'd just suggest to consider that third option to provide your credentials: request header . 我只是建议考虑使用第三个选项来提供您的凭据: request header

Edit : following appendix would just apply to any "external" middleware, like a proxy server or whatever, not a true express middleware inside sails.js 编辑以下附录仅适用于任何“外部”中间件,例如代理服务器或其他任何东西,而不是sails.js中的真正表达中间件

In addition, that would be a good idea that your middleware stripped those headers before redirecting to the real action. 另外,最好将中间件在重定向到实际操作之前剥离这些标头。

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

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