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
  • 2,005
  • 22
  • 70
  • 159
  • 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

3 Answers3

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,377
  • 3
  • 29
  • 52
-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,287
  • 1
  • 11
  • 17
-1
  1. Is a viable RESTful choice, but obviously has the limitations you have described.

  2. Don't do this. It would be construed by intermediaries as meaning “DELETE the (single) resource at /records/1;2;3” — So a 2xx response to this may cause them to purge their cache of /records/1;2;3; not purge /records/1, /records/2 or /records/3; proxy a 410 response for /records/1;2;3, or other things that don't make sense from your point of view.

  3. This choice is best, and can be done RESTfully. If you are creating an API and you want to allow mass changes to resources, you can use REST to do it, but exactly how is not immediately obvious to many. One method is to create a ‘change request’ resource (e.g. by POSTing a body such as records=[1,2,3] to /delete-requests) and poll the created resource (specified by the Location header of the response) to find out if your request has been accepted, rejected, is in progress or has completed. This is useful for long-running operations. Another way is to send a PATCH request to the list resource, /records, the body of which contains a list of resources and actions to perform on those resources (in whatever format you want to support). This is useful for quick operations where the response code for the request can indicate the outcome of the operation. Everything can be achieved whilst keeping within the constraints of REST, and usually the answer is to make the "problem" into a resource, and give it a URL. So, batch operations, such as delete here, or POSTing multiple items to a list, or making the same edit to a swathe of resources, can all be handled by creating a "batch operations" list and POSTing your new operation to it.

Please Note

REST isn't the only way to solve any problem. “REST” is just an architectural style and you don't have to adhere to it (but you lose certain benefits of the internet if you don't). I suggest you look down this list of HTTP API architectures and pick the one that suits you. Just make yourself aware of what you lose out on if you choose another architecture, and make an informed decision based on your use case.

Nadeem Taj
  • 117
  • 4
  • 21