Reputation: 43
Working on a simple API. Managed to get the GET & DELETE requests working, but I have some issues with the POST request. Pretty sure this is a noobie mistake, but I'm missing the logic here.
This is the file where all the routes are defined.
import express from 'express';
import fs from 'fs';
const router = express.Router();
'use strict';
let rawdata = fs.readFileSync('api/resources/vehicles.json');
let vehicles = JSON.parse(rawdata);
let vehiclesArray = vehicles.vehicles;
let data = JSON.stringify(vehicles);
fs.writeFileSync('api/resources/vehicles.json', data);
router.get('/', (req, res, next) => {
res.status(200).json(vehiclesArray);
});
router.get('/:id', (req, res, next) => {
let id = req.params.id;
res.status(200).send(vehiclesArray[(id-1)]);
});
router.delete('/:id', (req, res, next) => {
let id = req.params.id;
const removedVehicle = vehiclesArray.splice(id, 1);
res.status(200).json({
message: `Vechicle ${removedVehicle} with id ${id} was removed`
});
});
router.post('/', (req, res, next) => {
let id = vehiclesArray.length + 1;
const vehicle = {
"id" : id,
"type" : req.params.type,
"brand" : req.params.brand,
"model" : req.params.model,
"production_year" : req.params.production_year,
"kilometers_driven" : req.params.kilometers_driven,
"price" : req.params.price
};
res.status(201).send({
message: `POSTed new vehicle: ${vehicle}`,
vehicle: vehicle
});
res.status(201).send(vehiclesArray.push(vehicle));
});
export default router;
Inside my POST method I defined the vehicle, but once I try sending it I get the following in Postman, namely a confirmation of an added object and the object, but only with the id property
Can someone please give me a hint maybe on what I'm doing wrong? Do I also need a class declaration? Thanks!
Upvotes: 0
Views: 492
Reputation: 43
As advised by @Quentin: I installed the body-parser and included it in my code.
import bodyParser from "body-parser";
router.use(express.json());
router.use(bodyParser.urlencoded({ extended: true }));
Then I changed the req.param* to req.body* in the post function body.
router.post('/:id', (req, res, next) => {
let id = vehiclesArray.length + 1;
const vehicle = {
"id" : id,
"type" : req.body.type,
"brand" : req.body.brand,
"model" : req.body.model,
"production_year" : req.body.production_year,
"kilometers_driven" : req.body.kilometers_driven,
"price" : req.body.price
};
res.status(201).send({vehicle: vehicle});
res.status(201).send(vehiclesArray.push(vehicle));
});
As a last step, I changed the body type in Postman to x-www-form-urlencoded.
POST below , localhost:3000/vehicles/:id
GET below, just to check if the object was indeed added:
Also helpful in my endeavor: https://www.npmjs.com/package/body-parser
Upvotes: 0
Reputation: 944321
Look at the API documentation for params
:
This property is an object containing properties mapped to the named route “parameters”. For example, if you have the route /user/:name, then the “name” property is available as req.params.name. This object defaults to {}.
Now look at your route.
router.post('/', (req, res, next) => {
You don't have any params there. So that can't be the right thing.
Now look at your Postman screenshot you are filling out data on the tab labelled Body.
Go back to the API document and skim through the list of properties.
req.body
is listed there and says:
Contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as express.json() or express.urlencoded().
So set up a body-parsing middleware, then change the data you are POSTing so it is actually in that format (your screenshot shows you are entering raw data which is almost-but-not-quite-JSON so you need to fix that), make sure that you go into the Headers section and set the Content-Type
header to describe the format you are sending, then look at req.body
instead of req.params
.
Upvotes: 1