0

I am trying to send a body as content-type: text/plain but I am unable to retrieve it in my application. I am doing something like this:

"days": [1, 2],
"time": {
    "to":8,
    "from":12
}

But when i try to access it in my controller it doesn't show anything.

   async testFunction(@Body() body, @Req() req) {
       console.log(req.body)
       console.log(body)
   }

But both logs are blank. How do i get text in body ??

Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35
Tushar Roy
  • 1
  • 1
  • 16

1 Answers1

0

If you are sending a object you could simply use JSON instead of plain text:

POST replace_with_your_endpoint
Content-Type: application/json

{
    "days": [1, 2],
    "time": {
        "to":8,
        "from":12
    }
}

In one of your controllers:

@Post()
postRequest(@Body() body: any) {
  console.log(body) // { days: [ 1, 2 ], time: { to: 8, from: 12 } }
}
Lars Flieger
  • 2,421
  • 1
  • 12
  • 34
  • I wanted to send it as an object but we have problems in postgres where it takes {} as the notation for array. which raises another error of malformed array – Tushar Roy Aug 22 '21 at 07:48
  • @new_programmer I would convert it before inserting. How did you solve the issue? – Lars Flieger Aug 31 '21 at 11:13