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 {}

7
  • 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. Commented Sep 25, 2020 at 12:51
  • Is it possible that the issue would be related to axios? github.com/axios/axios/issues/3220 Commented Sep 25, 2020 at 22:54
  • Can you please add both server and client code? Commented Sep 26, 2020 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 Commented Sep 26, 2020 at 20:28
  • Can you state the version you are using? In addition, what is the data type of this.httpServiceInstance? Commented Sep 27, 2020 at 11:44

2 Answers 2

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,
})
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, I added this to the request but it's still not working. in microservice it's still empty
@Manspof this should work. Can you please provide me with a full project source code, so that I can reproduce your problem and try to help.
can we make private chat and I share to you?
@Manspof No, it should be publically available here, so that other potential users benefit from the answer
@Manspof you don't have to send your own full source code. Just make a dummy project with minimum required code for reproducing purpose only
|
-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.