How to add a key to json request in node js

Not applicable

My node.js file is:

var AWS = require('aws-sdk');
var apigee = require('apigee-access');
var http = require('http');
var util = require('util');
var url =  require('url');
var qs = require('querystring');
var server = http.createServer(function (request, response){
    var urlParts = url.parse(request.url, true),
        urlParams = urlParts.query, 
        urlPathname = urlParts.pathname
    console.log(urlPathname);
    console.log('Finished preprocessing');
    response.setHeader('Content-Type', 'application/json');
    AWS.config.accessKeyId="xxxxxxxxxxx";
    AWS.config.secretAccessKey="xxxxxxxxxxxxxxxxxxxxx";
    AWS.config.region ="ap-south-1";
    var lambda = new AWS.Lambda({region: 'ap-south-1', apiVersion: '2015-03-31'});
    var requestPayload = apigee.getVariable(request,'request.content');
    var params = {
        FunctionName: "helloname",
        InvocationType: 'RequestResponse',
		LogType:'Tail',
		Payload:requestPayload
    };
lambda.invoke(params,function(err, data) 
{
    console.log(requestPayload);
  if (err) 
  {
  console.log(err, err.stack); // an error occurred
  response.end('Error:'+err);
  }
  else     
  {
  console.log(data);           // successful response
  response.end(data.Payload);
  }
});
});
server.listen(9000, function () {
    console.log('Node HTTP server is listening');
});

My requestPayload is:

{"name":"abc"}

I want to add urlPathname as a new key called index into my requestPayload:

{"name":"abc"."index":"/xyz"}

How to do that?

0 5 1,368
5 REPLIES 5

Immediately after

var requestPayload = apigee.getVariable(request,'request.content');

add

requestPayload.index = urlPathname;

I already tried this one, but it didn't work.

mpurwar
Participant IV
	urlPathname = "/xyz"
	requestPayload = {"name":"abc"}

	requestPayload.index = urlPathname;
	requestPayload = JSON.stringify(requestPayload);

It works separately, but while trying to do in apigee it didn't work 😞

In Apigee, I would do the following, assuming you are trying to extract the proxy path and query params from the incoming request.

var server = http.createServer(function(request, response){
	urlParams = apigee.getVariable(request,'request.querystring');
        urlPathname = apigee.getVariable(request,'proxy.pathsuffix');
    
        requestPayload = apigee.getVariable(request,'request.content');
	requestPayload.index = urlPathname;
	requestPayload = JSON.stringify(requestPayload);