How to call HttpClientRequest in batch mode

Avatar

Avatar

JamesAlio

Avatar

JamesAlio

JamesAlio

19-01-2021

Hello, I'm working on a workflow where I have to call an external API using HttpClientRequest and all work fine.
The API that I call uses pagination so I have to call the API to get results for each page. I have 10 pages so I have to make 10 calls. I'm wondering if there is a way to make only one call to get the result instead of 10 calls.

this is my current code : 

 

var requestUrl = "https://www.muapiurl.com?";

// 10 calls
for (var i = 1; i <= 10; i++) {

var requestParam = "page="+i;
var url = requestUrl+requestParam;

var request = new HttpClientRequest(url);

var bufferEm = new MemoryBuffer();
request.header["Content-Type"] = "application/json; charset=utf-8";


request.method = "GET";

request.execute();

var dataResponse = request.response;

var data = JSON.parse(dataResponse.body);

for each(var obj in data) {
//.........
}
}

 

Regards.

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

wodnicki

MVP

Avatar

wodnicki

MVP

wodnicki
MVP

20-01-2021

Hi,

 

You can use keep-alive to reduce overhead, assuming server supports it:

Otherwise asynchronous mode can make the requests in parallel.

In the pasted code the MemoryBuffer is unneeded, and error handling should be added.

 

Thanks,

-Jon

Answers (0)