简体   繁体   中英

nestjs http method delete method send body data arrive empty

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

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,
})

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.

  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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM