Nitesh Rana
Nitesh Rana

Reputation: 512

req.body in nodejs is coming empty:{}?

This is my angular http request which makes a delete request when deleteEmployee function is called: This function is called on click of event:

$scope.deleteEmployee=function(index){
        $http({

            method:'DELETE',
            url:'/delete',
            data:{

            "ndx":"abc"

            }


        }).then((response)=>{
            console.log(response);
        })
    }

And this is my server.js file

var http=require('http');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));

app.use(bodyParser.json());


app.use('/',express.static(__dirname));

app.delete('/delete',function(req,res){

   console.log(req.body);

})

app.listen(8888,()=>{
    console.log('Server Started');
})

On console.log(req.body) it show empty i.e. {}.

Upvotes: 0

Views: 63

Answers (2)

sp00m
sp00m

Reputation: 48817

From https://www.rfc-editor.org/rfc/rfc7231#section-4.3.5:

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.

Basically, DELETE requests must not have a body.

Upvotes: 3

deepakchauhan
deepakchauhan

Reputation: 290

Try changing syntax :

$http.delete('/delete',{"ndx":"abc"});

Upvotes: 0

Related Questions