0

I am trying to test POST rest api from my app.

My package.json file:

{
  "name": "crypto_backend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "joi": "^17.4.0",
    "mongoose": "^5.12.12"
  },
  "devDependencies": {
    "nodemon": "^2.0.7"
  }
}

my index.js file:

const express = require("express");

const app = express();

app.use(express.json());

const courses = [
  { id: 1, name: "uday1" },
  { id: 2, name: "uday2" },
  { id: 3, name: "uday3" },
  { id: 4, name: "uday4" },
];

app.get("/api/courseslist", (req, res) => {
  res.send(courses);
});

app.post("/api/courses", (req, res) => {

    console.log(req.body);

  const course = {
    id: courses.length + 1,
    name: req.body.name,
  };
  courses.push(course);
  res.send(course);
});

app.listen(3000, () => console.log("listening"));

When I request for POST using postman chrome plugin and also vscode rest client extension, req.get is not getting any parameters and the name field is taken as empty.

But I passed the request as below.

POST http:/localhost:3000/api/courses

{
    "name": "sample"
}

with raw data selected in postman plugin and passed above data.

uday
  • 569
  • 1
  • 6
  • 16
  • can you confirm the post endpoint is hit? have you set the `Content-Type` header in the request? – germanio May 28 '21 at 19:44
  • post request is hit and an entry also created in the list but without the parameter. I am creating id and name fields. Id is created autoamtically and name is passed as parameter, there it is showing as empty. Even I kept console.log(req.body), there it is empty brackets {}. – uday May 28 '21 at 19:50

1 Answers1

0

Beyond express.json, you need to add express.urlencoded to parse the body:

const express = require("express");

const app = express();

app.use(express.json());
app.use(express.urlencoded({extended:false}));

...

The express.urlencoded() function is a built-in middleware function in Express. It parses incoming requests with urlencoded payloads and enable us get req.body.

If you want to get the data from a form instead Postman, you don't need set any header because the browser send all necessary headers for you when user submits the form. So, a simple traditional form like this should work:

<form action="/api/courses" method="post">
  <input type="text" name="name" />
</form>
Cássio Lacerda
  • 1,554
  • 1
  • 12
  • 14
  • it seems postman issue, it is not setting the header, after I kept application/json as Content-Type, it is working. There is one more question I posted where I am passing the same request from a ejs template form. How to set these things in the form? – uday May 28 '21 at 20:04
  • I add more information. I hope it help you. – Cássio Lacerda May 28 '21 at 20:13
  • Yeah I tried with both and true and false. Can you please check this question. https://stackoverflow.com/questions/67741580/request-body-not-getting-data-even-after-url-encoding-in-nodejs-how-to-fix-that – uday May 28 '21 at 21:13