Hugoren Martinako
Hugoren Martinako

Reputation: 490

Angular $http.delete returns error 422 (Unprocessable Entity)

I am getting this frontend issue when my webpage tries to call a DELETE-rest method. But, the fun part is the backend works perfectly when I do the SAME call but using SoapUI. Here's my function call:

$scope.remove = function (id) {
     var delUrl = "http://localhost:8080/secure/regulations/" + id;
     $http.delete(delUrl);
}

The web service is like secure/regulations/{id}, and give no answer (just do a delete), and as I said, a SoapUI call works like a charm, but a this function in the browser doesn't. Here below the headers:

General
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/secure/regulations/4
Request Method:DELETE
Status Code:422 Unprocessable Entity

Response Headers
Content-Type:application/json;charset=UTF-8
Date:Tue, 23 Jun 2015 14:28:00 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked

Request Headers
Accept:application/json, text/plain, *\/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:__ngDebug=true
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/secure/

Taking a deep look in the backend's function, the problem raises when it does a get(id) against the database (before that point, id has value), but I resist to believe that the problem is there if SoapUI works. Something may be missed at frontend code :S

EDIT: In SoapUI, request raw is the following:

DELETE http://localhost:8080/secure/regulations/5 HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/json
Content-Length: 0
Host: localhost:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

There is no headers at all (in headers tab), but in Representations there is an empty Media-Type: application/json (autocreated).

Any help is honestly grateful!

Upvotes: 3

Views: 4328

Answers (1)

Andrei Mărcuţ
Andrei Mărcuţ

Reputation: 1313

Try using this debug-verbose version with extra header similar to SoapUI request:

$scope.remove = function (id) {
    var delUrl = "http://localhost:8080/secure/regulations/" + id;
    var _delete = {
         'method': 'DELETE',
         'url': delUrl,
         'headers': {
             'Content-Type': 'application/json'
         },
         'data': ""
    }

    $http(_delete)
        .success(function(){
            console.log("OK");
        })
        .error(function(data, status, headers, config){
            console.log(data);
            console.log(status);
            console.log(headers);
            console.log(config);
        });
}

Upvotes: 1

Related Questions