1

I'm doing an http.request and in case of a server response error 500, I would like to access the data the was sent during the request in order to retry the request. How do I access the requestBodyData parameter from the response callback scope?

see below:

var arrayofobjects = [
    {
        "name" : "peter",
        "age" : "24"
    },
    {
        "name" : "gleice",
        "age" : "19"
    },
    {
        "name" : "pedro",
        "age" : "34"
    }
];

for(var x=0; x<arrayofobjects.length; x++){
    sendModelInputData(arrayofobjects[x]);
}

function sendModelInputData (requestBodyData) {

    const req = https.request(connectionInfo, (res) => {

        logger.info(`STATUS: ${res.statusCode}`)
        logger.debug(`HEADERS: ${JSON.stringify(res.headers)}`)

        //how to access requestBodyData from here????

        res.setEncoding('utf8');

        res.on('data', (chunk) => {

            logger.info(`BODY: ${chunk}`);

        });

        res.on('end', () => {
            //console.log('No more data in response.');
        });
    });

    req.on('error', (e) => {
        //console.error(`problem with request: ${e.message}`);
        logger.error(`problem with request: ${e.message}`);
    });


    //req.end(postData);
    req.end(JSON.stringify(requestBodyData));

}
vilelam
  • 804
  • 1
  • 11
  • 19
  • 1
    You can just access it normally. Learn about closures. – SLaks Apr 24 '18 at 18:09
  • Hi Slaks - I'm calling the sendModelInputData in a loop so the requestBodyData parameter is changing and from the response callback I'm not able access the right data – vilelam Apr 24 '18 at 18:13
  • Post your full code, unless you're mutating `requestBodyData`, sending it again will work. – Marcos Casagrande Apr 24 '18 at 18:16
  • Hi Marcos thanks for your prompt reply. Sending requestBodyData works, what I need is to access this data from the response callback scope. Note that the callback will happen when the server returns the response and by that time the requestBodyData variable can have another value since the code above is called in loop. – vilelam Apr 24 '18 at 18:19
  • If you want us to help you, with the given code, you can send `requestBodyData` in the response callback without any issues. So post the loop you're talking about and we will be able to help you. Because you're mutating the object somewhere else in the code, that's why it isn't working. – Marcos Casagrande Apr 24 '18 at 18:21
  • @MarcosCasagrande, see the full code above. thanks in advance – vilelam Apr 24 '18 at 18:35
  • So you're sending a random number to the endpoint, that's not the code. otherwise requestBodyData will remain unchanged. – Marcos Casagrande Apr 24 '18 at 18:37
  • requestBodyData will change its value 10 times and I will send 10 different values to the endpoint. the problem is how do I access from the response callback the value that was sent in the request. – vilelam Apr 24 '18 at 18:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169699/discussion-between-marcos-casagrande-and-vilelam). – Marcos Casagrande Apr 24 '18 at 18:44

2 Answers2

1

From your comments I realized you're changing requestBodyData outside of sendModelInputData, so you have to clone it to keep the original value. You can achieve that using Object.assign.

function sendModelInputData (requestBodyData) {
    requestBodyData = Object.assign({}, requestBodyData); // Shallow clone

    // On retry use requestBodyData which will be the same data you sent
}

Check the following question, in order to understand what is happening.

Javascript by reference vs. by value

Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
-1

var reqBody = res.request.body.toString(); reqBody = JSON.parse(reqBody);