Receive parameter in delete request

Asked

Viewed 1,320 times

6

I am trying to pass a parameter to my DELETE request, I tried to do the following:

Use in the same way I use in GET (where it works normal).

app.delete('/contatos', function(req, res){
    var obj = req.body;
    console.log(obj);
});

But on the island of Nodejs this .log() prints only {}.

I also tried this way (found in Soen):

app.delete('/contatos',function(req, res){
    var obj = req.body.data;
    console.log(obj);
});

But the same thing happened.
Maybe the DELETE implementation is different from GET and POST and I ignored some step.

I am requesting the API from Angularjs, with the following service:

angular.module("lista").factory("contatosAPI", function($http){     
    var _saveContato = function(contato){
        return $http.post("http://localhost:3412/contatos", contato);
    };

    var _deleteContato = function(contato){
        console.log(contato); //Aqui o objeto está normal
        return $http.delete("http://localhost:3412/contatos", contato);
    };

    return {
        saveContato: _saveContato,
        deleteContato: _deleteContato
    };
});

In the controller I call the service, this way:

$scope.apagarContatos = function(contato){      
    contatosAPI.deleteContato(contato);
};
  • How are you making the requisition?

  • @Rafaeltelles edited

1 answer

2


the delete service of the $http method, returns you a promisse ie, to access server return data, must be implemented as follows.

the delete service does not accept an OBJECT to be sent, because the same at the end performs a Get to the server, ie the information must go in the URL, so you can only pass parameters, and not a complete object.

 $http.delete("http://localhost:3412/contatos/" + contato.ID).success(function (data, status) {
                console.log(data); // Retorno seu Data
            });
  • I made a change with demostration for both a request example with parameters and Response callback sucess @Jéfersonbueno

  • I did not call twice, I made an example request with params, and an example of feedback, I will edit .. what I’m trying to say is that the request delete is like a GET, you can not pass an object only parameters by the url this url would be http://localhost:3412/contacts/1 <-- 1 (ID), you can not recover the object from the other side understood ?

  • Good, I got it. And there’s some way to send several ids in the same url? Something like, http://localhost:3412/contatos/1+2+3, to send the ids 1, 2 and 3

  • Man what I would do, instead of using $http.delete, I would use $http.post where as a parameter I would implement to receive a list of Ids (List<String>) in my case C# - webapi2 (server), and inside I would treat my rule to perform the Letes understand ?

  • worked out ? @Jéfersonbueno

  • 1

    você não consegue recuperar o objeto do outro lado - This is not completely true. Depending on the implementation, the endpoint can perform a GET before, to get the value to be deleted, prepare it for return, and then delete the ORM element.

  • 1

    @Onosendai, if you are working service oriented, you have a JSON or Object on the Client side, on the server side you have no information, using the $http.delete service it cannot pass a JSON or Object as parameter, what it can do is put each property as parameter of the url, which is not passing the object itself, but parameter to parameter, now on the server side there are N implementations that can be performed with this information, but using $http.delete() I believe it’s the only way out, yes.

  • 1

    @Renandegrandi What you just described agrees with what I mentioned. I did not refer to the values mapped between the request and endpoint (where, correctly, you pointed out that only one propriate is passed - the identifier) but regarding the implementation, which can rather return the serialized representation of the object that was deleted.

  • Of course, I referred to Request and not Response when I said (retrieve the object on the other side) "server", in Sponse I can return what I want and recover in Promisse. @Onosendai. I think it was a miscommunication.

  • 1

    @Renandegrandi No stress, just wanted to clarify this point for a future reader of your answer. Just out of curiosity, the $http.delete allows the passage of a complete object ($http.delete('/endpoint/'+ id, {prop1: val1});) - but it’s a antipattern - the whole object goes serialized in querystring.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.