AngularJS and ExpressJS: how to read content sent by $http.put() AngularJS and ExpressJS: how to read content sent by $http.put() express express

AngularJS and ExpressJS: how to read content sent by $http.put()


Try this

$http({   method  :'PUT',     url:'http://127.0.0.1:3000/s',     params: {"test":true, _method: 'PUT'},     headers :{'Content-Type':'application/json'}        }).success(function(data, status, headers, config) {            alert('OK');        }).error(function(data, status, headers, config) {            alert('error');        }).catch(function(error){            alert('catch' + JSON.stringify(error));        });


I have finally found the problem:

What happens is, that the browser makes a preflight OPTIONS HTTP request, and if it receives status 200 from server, than in proceeds to the original PUT call.

On the ExpressJS server, following configuration were made and the PUT request from AngularJS is now correctly sending and receiving data using PUT request:

// apply this rule to all requests accessing any URL/URIapp.all('*', function(req, res, next) {    // add details of what is allowed in HTTP request headers to the response headers    res.header('Access-Control-Allow-Origin', req.headers.origin);    res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');    res.header('Access-Control-Allow-Credentials', false);    res.header('Access-Control-Max-Age', '86400');    res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');    // the next() function continues execution and will move onto the requested URL/URI    next();});

It is still a mystery to me, why POST request were correctly processed and PUT requests were not


Your server "Accept":"application/json and you are sending "Content-Type":"application/x-www-form-urlencoded" so you must use 'Content-Type': 'application/json' and transform your data to json, like this:

function youMethod(){            var myUrl = 'http://127.0.0.1:3000/s';            var data = {                test: true,                _method: "PUT"            };            return $http({                url: myUrl,                method: 'PUT',                data: JSON.stringify(data),                 headers: {                    'Content-Type': 'application/json'                }              }).then(youMethodComplete)                .catch(youMethodFailed);            function youMethodComplete(response) {                alert('OK');                console.log(response);            }            function uyouMethodFailed(response) {                alert('ERROR');                console.log(response);                 }        }

pd: I recommend too -> don't use $http success

Extra: If you want use "Content-Type":"application/x-www-form-urlencoded" and you configure your server for this purpose and you want to follow this syntax:

function youMethod(){                var myUrl = 'http://127.0.0.1:3000/s';                var data = {                    test: true,                    _method: "PUT"                };                return $http({                    url: myUrl,                    method: 'PUT',                    data: $httpParamSerializer(data), //remember to inject $httpParamSerializer                    headers: {                        'Content-Type': 'application/x-www-form-urlencoded'                    }                  }).then(youMethodComplete)                    .catch(youMethodFailed);                function youMethodComplete(response) {                    alert('OK');                    console.log(response);                }                function uyouMethodFailed(response) {                    alert('ERROR');                    console.log(response);                     }            }