Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

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,459
5 REPLIES 5