0

I am using following code inside azure http trigger function.

var form = new FormData();
var fetch = require("node-fetch");
var https = require('https');
var httpAgents_ = new https.Agent({keepAlive: true});
httpAgents_.maxSockets = 200;
module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

   let form_ = getMultiData(context);

   const response = await getResponse(form_,context);

    context.res = { 
           body: response };
    context.done;
};
async function cerenceLookResponse(body_,context)
{
  context.log('calling http function')
   const url = "https://webhook.site/5c7950af-85fc-49b5-a677-12430805a159";
    return fetch(url, {
        method: 'POST',
        body: form,
        headers: {
            "Accept": "*/*",
            "Accept-Encoding": "gzip, deflate, br",
            "Connection":"Keep-Alive",
            "Content-Length":1330,
            "Keep-Alive": "timeout=30000",
            "Transfer-Encoding": "chunked",
        },
        agent: httpAgents_
        
        }).then(res => res.text());
};
function getMultiData(context){
    var content = { "cmdDict":{ "device": "iso", };
    var content2 ={"appserver_data": {"action_mode": "default",  },"enable_filtering": 1,  };
    var options = { header: {'content-type': "application/JSON; charset=utf-8"}};

    form.append('RequestData', JSON.stringify(content), options);
    form.append('DictParameter', JSON.stringify(content2), options);

    var ar = []
    for(var i=0;i<54;i++){
        ar.push(form._streams[3][i]); }
    var ar2 = []
    for(var i=0;i<52;i++){
        ar2.push(form._streams[3][i]);}
    boun = ar2.join("")
    test1_orig = ar.join("")
    test2 = "Content-Disposition: form-data; name=\"DictParameter\"; paramName=\"REQUEST_INFO\""
    test1 = test1_orig + test2 + "\r\n" +'content-type: application/JSON; charset=utf-8\r\n' + '\r\n',
    form._streams[3] = test1
    form._streams.push(boun + "--\r\n");
  
    return form;

};

This program perfectly fine when I work with a local WebStorm terminal. However, on Azure portal first time it gets the response from the web hook and then if I want to run once again right after I get the response. The function stall and after 3 min and throws an error "socket hang upStack: FetchError". What am I doing wrong here?

1
  • What is in the getResponse function? Also, why are you creating form variable in the global scope and not locally in getMultiData function? Commented Nov 23, 2020 at 16:02

1 Answer 1

1

You are creating form in the global scope. So every time you are calling the getMultiData function, duplicate stuffs getting added in the same FormData instance. Declare it locally inside getMultiData function.

Sign up to request clarification or add additional context in comments.

2 Comments

I have a question, if we declare a global variable in Azure function will it be reinitialize every time a http request call the function or it will be there as long as we clean the memory or restart the function ?
Azure Function would manage the instance creation. Hence it can reuse old instance at times when invoked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.