In my last blog, I shared a Node.js REST API client to create hundreds of resources (B2Bi trading partners) for testing purposes. In this blog I created a Node.js program that will delete the resources, created previously, in bulk.
This program requires the node-rest-client and async Node.js modules.
$ npm install node-rest-client
$ npm install async
In this example I used the REST API of IBM Sterling B2B Integrator to delete a 200 Sterling FileGateway Trading Partners in this format: Partner### .
You can use this Node.js code with any other REST API.
deleteMultiplePartners.js
var Client = require('node-rest-client').Client; var async = require('async'); //TODO: Change var address = "http://x.x.x.x"; var port = "5074" var apiUri = "/B2BAPIs/svc/" var baseUri = address + ":" + port + apiUri; //TODO: Change var auth_info = {user:"user", password:"password"}; //Create client instance with auth info client = new Client(auth_info); //Define required headers var headers = { "Content-Type": "application/json", "Accept": "application/json" }; // set content-type header and data as json in args parameter var args = { headers: headers }; var i = 0; var continueWhilst = true; async.whilst( function test() { return continueWhilst; }, deletePartner, function (err) { } ); function deletePartner(next) { client.delete(baseUri + "tradingpartners/Partner"+i,args, function (data, response) { // parsed response body as js object console.log(data); // raw response //console.log(response); } ); i += 1; if (i === 200) { //stop iterating continueWhilst = false; } console.log("Iterating, i= " + i); // This is in the callback for request now. next(); };
To run the program: save the code into a file and run:
node deleteMultiplePartners.js
Leave a Reply