I have a resource 'roles' which has a many to many relationship with 'user'. To administer 'roles' I need to send the role id and the user id to the server so that it removes the the role from the correct user (not necessarily the logged in user)
Here is what I was attempting but according to the docs this is not possible. I know I can send the two ids in the uri but my laravel backend automatically sets up a resourceful route of resource/{resourceid} which I would like to use if possible. Is there a way to do this that I am missing?
var removeRole = function (roleid, userid) { var input =[]; input.user = userid; $http.delete('/roles/' + roleid, input).success(function (data, status) { console.log(data); }); };
By this it seems optional whether you want to provide a body for a DELETE request. The RFC states that: A payload within a DELETE request message has no defined semantics; sending a payload body on a DELETE request might cause some existing implementations to reject the request.
The HTTP DELETE method is used to delete a resource from the server. Unlike GET and HEAD requests, the DELETE requests may change the server state. Sending a message body on a DELETE request might cause some servers to reject the request. But you still can send data to the server using URL parameters.
get request Method Syntax: $http. get(url, { params: { params1: values1, params2:values2, params3:values3...... } });
The DELETE method requests that the origin server delete the resource identified by the Request-URI. This method MAY be overridden by human intervention (or other means) on the origin server.
AngularJS Http Delete ($http.delete()) Service Method. The $http.DELETE() method in angularjs is used to delete a resource on the server based on given specific URL. Its short cut method of $http services in angularjs. Generally $http.delete method rarely allowed to delete a resources on the server due to security reasons.
It is used to define a required operator like get or send data. In angularjs we have different methods available. We need to send url of http server to perform required operations. In above syntax we passed $http as argument to the controller.
$http ( angular.merge ( {}, config || {}, { method : 'delete', url : _url, data : _data })); A many to many relationship normally has a linking table. Consider this "link" as an entity in its own right and give it a unique id, then send that id in the delete request.
But in a real-world application it soft deletes the data, meaning data is not be removed from the server permanently, it just gets inactivated, mainly for reporting purposes. To make the HTTP request, you should always look for the headers because in many cases the server will only accept the request from an authorized client.
You can do an http DELETE via a URL like /users/1/roles/2. That would be the most RESTful way to do it.
Otherwise I guess you can just pass the user id as part of the query params? Something like
$http.delete('/roles/' + roleid, {params: {userId: userID}}).then...
My suggestion:
$http({ method: 'DELETE', url: '/roles/' + roleid, data: { user: userId }, headers: { 'Content-type': 'application/json;charset=utf-8' } }) .then(function(response) { console.log(response.data); }, function(rejection) { console.log(rejection.data); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With