0
const express = require("express");
const app = express();
const oracle = require("oracledb");
const bodyParser = require("body-parser");

const urlEncodedBodyParser = bodyParser.urlencoded({ extended: true });
app.use(express.json());
app.post("/addPlacement", urlEncodedBodyParser, function (request, response) {
  console.log(request.body);
  console.log(request.body.id);
  /*
  console.log(request.body.name);
  console.log(request.body.placementType);
  console.log(request.body.company);
  console.log(request.body.salary);
  console.log(request.body.salaryType);
  */
  // console.log(request);

  response.send(request.body);
});

app.listen(5050, function (err) {
  if (err) {
    console.log(err);
  }
  console.log("Server is ready to request on port 5050");
});

If I console log the request.body it shows undefined and sends the empty object and in case console.log(request) I get this error.

Advanced REST Client request (image)

stranger
  • 29
  • 1
  • 1
  • 5
  • Did you know you no longer need body-parser. You can use `express.urlencoded()` instead – Phil May 10 '22 at 05:00
  • As you've noticed, you cannot serialise the `request` object as JSON due to its circular references. What did you _actually_ want to respond with? What should the response data be? If `request.body` is empty, it means you aren't posting any data from the client. How are you making the request? – Phil May 10 '22 at 05:03
  • Yes, I am using Advance Rest Client for sending the request using application/www-form-unencoded – stranger May 10 '22 at 06:34
  • If the request.send(request.body) is written then the response should be the object of data we are trying to send like {id: 109, name: David} but it shows undefined. I m sharing you the screenshot of it – stranger May 10 '22 at 07:02
  • Yes, that's correct. But that's not what's in your code. You have `res.send(request)` which won't work – Phil May 10 '22 at 07:04
  • I have attached screenshot of request. In that those {} object is empty and undefined in console bar – stranger May 10 '22 at 07:20
  • You still have `response.send(request)` in your code. I think you want `response.send(request.body)` or `response.json(request.body)` – Phil May 10 '22 at 07:22
  • It's not clear where you set the request content-type in that client. Are you sure it's sending `application/x-www-form-urlencoded` and not `multipart/form-data`? Perhaps you want to try a different client like [Postman](https://www.postman.com/) – Phil May 10 '22 at 07:25
  • Yes i want response.send(request.body) i wrote that mistakenly – stranger May 10 '22 at 07:26
  • I 'm sending application/x-www-form-urlencoded – stranger May 10 '22 at 07:28
  • Could you please explain or show with another screenshot where and how the content-type for the request is set? I can't see it anywhere in the screenshot you've provided – Phil May 10 '22 at 07:37
  • 1
    I tried to use postman. It's working – stranger May 10 '22 at 08:53

0 Answers0