3

I build nestjs project with microservices and I'm trying to send body data from HTTP method "delete" and get the data from req.body. that data is arrive empty.

nestjs project

  await this.httpServiceInstance
    .delete(`deleteData`, {
    data,
    })

microservice project

routes

  app.delete("/deleteData", endpoint(deleteData));

function deleteData

        module.exports = async (req) => { console.log(req.body) /* more code*/ } 

it's print empty object {}

Manspof
  • 598
  • 26
  • 81
  • 173
  • There could be multiple reasons for it, could you please monitor network on the server or container and check if it is even reaching to the server? If it is, may be something on you server app could be the reason may be deserialization in wrong format. – Dipen Shah Sep 25 '20 at 12:51
  • Is it possible that the issue would be related to axios? https://github.com/axios/axios/issues/3220 – Jafar Akhondali Sep 25 '20 at 22:54
  • Can you please add both server and client code? – manishg Sep 26 '20 at 19:43
  • that's part of my code. from client I use postman to simulate.. other request works good. only delete method with body not accept the body in microservice – Manspof Sep 26 '20 at 20:28
  • Can you state the version you are using? In addition, what is the data type of `this.httpServiceInstance`? – ggordon Sep 27 '20 at 11:44
  • The `delete` method could be sending the request with the wrong headers ? Similar to this issue https://stackoverflow.com/questions/37796227/body-is-empty-when-parsing-delete-request-with-express-and-body-parser – Morphyish Sep 28 '20 at 09:38
  • @ggordon httpServiceInstance is type of HttpService from @nestjs/common – Manspof Sep 29 '20 at 06:51

2 Answers2

1

Please set $httpProvider in your config this way:

$httpProvider.defaults.headers.delete = { "Content-Type": "application/json;charset=utf-8" };

and then call delete request:

await this.httpServiceInstance
.delete(`xxxxx`, {
data,
})
Michel Gokan Khan
  • 2,525
  • 3
  • 30
  • 54
-1

I think you're using express, although I don't see it explicitly stated. It needs a body parser, for example:

const express = require('express')
const app = express();
const port = process.env.PORT || 3000;

// parse body as JSON, puts object into req.body
app.use(express.json());

app.route('/deleteData')
.all((req, res) => {
    console.log(req.method, req.body);
    res.json(req.body);
});

app.listen(port, function() {
    console.log('Server started on port: ' + port);
});

Start the server, then test:

curl -X DELETE --data '{"foo": "bar"}' -H 'Content-Type: application/json' http://localhost:3000/deleteData

Server console shows: DELETE { foo: 'bar' }, returns {"foo":"bar"} to the client.

mike.k
  • 3,277
  • 1
  • 12
  • 18