I am using Nodejs with Hapi for rest api implementation and backend DB is mongo. I have a requirement where user can pass parameters in request payload for a GET call and I need to search accordingly based on those values. In Hapi framework I do not get any payload value when I do a GET method. Is there a way I can get payload. Below is my code.
route:
{
method: "GET",
path: `/v1/getProducts`,
handler: function(request, reply) {
ProductController.getProducts(request, reply);
}
}
ProductController:
const mongoose = require("mongoose");
const Products = require("../models/products");
const getProducts = (req, res) => {
console.log("I am in getProducts");
console.log("Headers :", req.headers);
console.log(req.payload);
var query = {};
//If values present in request payload for a query accordingly
if (req.payload.productName) {
query["PRODUCT_NAME"] = req.payload.productName;
}
if (req.payload.productType) {
query["PRODUCT_TYPE"] = req.payload.productType;
}
if (req.payload.supportHardware) {
query["HARD_SUPP"] =
req.payload.supportHardware;
}
Products.find(query, "-_v")
.exec()
.then(results => {
console.log(results);
res({ result: "Success", ProductList: results });
})
.catch(err => {
console.log("GETPRODUCTS RESPONSE: ", err.stack);
res({ error: { errorId: "xyz", message: err } });
});
};
module.exports = { getProducts };
But I null value in req.payload
I am in getProducts
Headers : { host: 'localhost:8000',
'user-agent': 'curl/7.63.0',
accept: '*/*',
'content-type': 'application/json',
authorization: 'Basic bTE3NTg5h1Yi5hdHQuY29tOmJyaWFuSXNDb29sMTg=',
'content-length': '34',
SERVICENAME: 'rest/productService/v1/getProducts',
REQUEST_START_TIME: 1548272230344,
'X-CSI-ConversationId': 'anscUser~CNG-CSI~1d58d6fd-a981-4bba-b9ed-16964d7cc51b',
'X-CSI-UniqueTransactionId': 'AnscCsiRestful38856@e1f99082-150e-4b1e-b92f-04bb6c007d65',
'X-CSI-MessageId': '1b06946e-732d-4075-ba6a-2dc7d13f4f80',
'X-CSI-TimeToLive': '60000',
'X-CSI-SequenceNumber': '1',
'X-CSI-TotalInSequence': '1',
'X-CSI-ClientApp': 'ansc-csi-restful~N/A',
DOWNSTREAM_CALLS_INFO: [] }
null