One of the controller methods in my NestJS application is supposed to take plain text as its body but whenever I try to make a request, the parameter is received as an empty object. Is this even possible or am I going to have to create some sort of DTO to pass that single string?
Example:
@Post()
myFunction(@Body() id: string) {
// do something here
}
The semantics of a post request are determined by the header that indicates the content type. Try making sure the request header has the type 'text/plain' and see of this helps.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
I see that this question is pretty old, but it is listed in google among first, so I want to add answer here.
If you don't want to add
body-parser
middleware (for example, you want plain text only in single controller method), you can useraw-body
(which is already exists in your node_modules), something like this:you could also create new parameter decorator
and use it like
(I didn't check decorator code, wrote it right here in comment)
Nest is not compatible with plain/text and you must pass a bodyparser to your express app instead. Try something like this:
where options is created from https://www.npmjs.com/package/body-parser