Passing request-promise array to Promise.all()?

18 views
Skip to first unread message

Lucky Jindal

unread,
Jan 10, 2018, 9:34:22 PM1/10/18
to nodejs
How can I make HTTP POST call to a server using array of request-promise objects where URL is same, only data is different each time.

I have array of objects that need to be POST'ed to a server one by one, independently. I created request promise's array and then promise.all () but failed. However, single object (not using array) works.



Ex: 1. Previous functionality, with 1 object, I use -

var request = require("request-promise")
           options.body ={ "name" : "sdsd"}
request (options).then(...).catch(..) -> it works


2. Now with many objects in array, I created objectsArr[],  then I use promise.all() or q package, q.all (), it fails with error message from external server, saying invalid json/JSON parsing error.
 
           objArr[] =[{"name":"dsd"},
                 {{"name":"dsd"},}]

var reqPromises[];
 
objArr.forEach(function(obj){
      options.body= obj
     reqPromises.push(request(options)
}
then-> 
q.all(reqPromise).then(..).catch(..)


Even if object Array has just 1 object, it is  an array  structure which is causing issues. I do not know how it is working, if it is trying to make 1 http request with arrays in json and henec failing. If in loop, means making multiple POST requests, which I want, it should work. 

Please suggest the right approach. Also if any better way to debug and see what JSON is going to server? I use postman to call my local application which talks and sends json to external server.  Usually console/log statements are useful but with request/promises/chaining, its hard to debug. :-(

Lucky Jindal

unread,
Jan 12, 2018, 2:52:13 PM1/12/18
to nodejs
Resolved the issue. I had JSON.stringify in my options_1.body which was not needed. Please consider it resolved. I do ont know if there is any option I need to click to mark it resolved as in other forums. 



On Wednesday, January 10, 2018 at 9:34:22 PM UTC-5, Lucky Jindal wrote:
How can I make HTTP POST call to a server using array of request-promise objects where URL is same, only data is different each time.

I have array of objects that need to be POST'ed toa server one by one, independently. I created request promise's array and then promise.all () but failed. However, single object (not using array) works.
Reply all
Reply to author
Forward
0 new messages