0

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

1 Answers1

1

It finally occurred to me that you are passing a payload in a GET request. This is not something you should be doing. You should be passing payload data/lookup values, as query params. Alternatively, you should consider using a POST request instead. Here is some additional info on this topic.

In your case, if you want to continue to use a GET request, you should use the request.query to access the values. Docs here

Something like the below will loop through all the passed in query params and build you a lookUp query to be used in your db lookup. You might want to add a filter or validate the key names as you wouldn't want random keys to be used as lookup fields in your db request

The below replaces req.query with query.

var query = {"PRODUCT_NAME": 123, "PRODUCT_TYPE": 456};

var lookupQuery = Object.keys(query).reduce((lookUp, key) => {
  lookUp[key] = query[key]
  return lookUp
}, {})

console.log(lookupQuery);
Rastalamm
  • 1,712
  • 3
  • 23
  • 32
  • Yeah! I agree that payload with GET method is not the perfect way. But I am not in control of changing the requirement. Max i can propose to use POST/PUT instead of GET. But I want to know if there is a possibility to get hapi server accept the payload in GET and pass it for use. How can I make that possible – mudumbi acharya Jan 24 '19 at 09:54
  • It's not possible for Hapi to 'read' the payload for `GET` requests. Below is the code from Hapi that processes `GET` req's and binds the payload. `exports.payload = function (request) { if (request.method === 'get' || request.method === 'head') { // When route.method is '*' return; } return internals.input('payload', request); };` As you can see, if the method is `get` or `head`, it just returns and does not do anything with the `payload` – Rastalamm Jan 26 '19 at 19:44