简体   繁体   中英

Request module: Is this the correct way to make consecutive POST requests with different body's?

So I am using the NodeJs "request" module to make multiple post requests, and I feel as though I may be doing it incorrectly. Is there a better way to approach this?

Lets say I have an array of different JSON bodies I need to send.

        var requests = [
                {
                    "Subject": "TestSubject1",
                    "Body": "TestBody1",
                    "PreferredLanguage": "en-US",
                    "Recipients": [
                        {
                            "FirstName": "TestName1",
                            "EmailAddress": "TestEmail1"
                        }
                    ]
                },
                {
                    "Subject": "TestSubject2",
                    "Body": "TestBody2",
                    "PreferredLanguage": "en-US",
                    "Recipients": [
                        {
                            "FirstName": "TestName2",
                            "EmailAddress": "TestEmail2"
                        }
                    ]
                },
                {
                    "Subject": "TestSubject3",
                    "Body": "TestBody3",
                    "PreferredLanguage": "en-US",
                    "Recipients": [
                        {
                            "FirstName": "TestName3",
                            "EmailAddress": "TestEmail3"
                        }
                    ]
                }
            ]

Here is how I am attempting to do it.

         for(var i = 0; i < requests.length; i++){
                request.post(
                    {
                        url: 'https://testURL.com/messages',
                        json: requests[i],
                        method: 'POST'
                    },
                    function (e, r, body) {
                        console.log(body);
                    }
                )
            }

I have a feeling this is a very bad way of approaching this problem. What is the correct way to do this?

You can do it that way. Generally speaking, you want to avoid making tons of HTTP calls, so if you have the ability to edit the API you are making the requests to, you could change the endpoint to take an array (your current requests array) and return whatever you need all in one HTTP request.

You should not blindly make hundreds of parallel requests to a server.

You should have some limit on number of parallel request to a server other wise server might get down or server might block you as well

I personally use eachLimit function of node's async module in these type of situations.

Following is a working code

const async = require('async');
const request = require('request');
var limit = 2 //max 2 parallel request at time

var requests = [
    {
        "Subject": "TestSubject1",
        "Body": "TestBody1",
        "PreferredLanguage": "en-US",
        "Recipients": [
            {
                "FirstName": "TestName1",
                "EmailAddress": "TestEmail1"
            }
        ]
    },
    {
        "Subject": "TestSubject2",
        "Body": "TestBody2",
        "PreferredLanguage": "en-US",
        "Recipients": [
            {
                "FirstName": "TestName2",
                "EmailAddress": "TestEmail2"
            }
        ]
    },
    {
        "Subject": "TestSubject3",
        "Body": "TestBody3",
        "PreferredLanguage": "en-US",
        "Recipients": [
            {
                "FirstName": "TestName3",
                "EmailAddress": "TestEmail3"
            }
        ]
    }
]

function iteratee(requestBody, cb) {
//this will be called for each item in the requests array
    console.log("request processing starts")
    request.post({
            url: 'https://testURL.com/messages',
            json: requestBody,
            method: 'POST'
        },
        function (e, r, body) {
            console.log(e);
            console.log(body);
            cb();
            console.log("request processing ends")
        });
}

function finalCallback(err) {
    //this will be called when all the requests are processed
}

async.eachLimit(requests, limit, iteratee, finalCallback)

All depends on your intentions:

You're fine if you need to process requests' results separately (all requests are asynchronous and will be handled by passed callback function separately).

But often you need to make final action when all of our requests have completed . You can use Promise.all for this.

For example:

require("request/package.json"); // request is a peer dependency.
const requestPromiseAny = require("request-promise-any");

const requests = [
  {
    "name": "morpheus",
    "job": "leader"
  },
  {
    "name": "tom",
    "job": "leader"
  }
];
Promise.all(requests.map((requestJSON) => {
  return requestPromiseAny.post(
    {
      url: 'https://reqres.in/api/users',
      json: requestJSON,
      method: 'POST'
    });
})).then((responses) => {
  console.log(responses);
});

You can easily try it on runkit .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM