I wrote this Node.js REST API client to create hundreds of trading partner for testing purposes. It will help anyone who works with Node.js and REST API to create resources automatically and quickly for load testing or simulating performance problems.
This program requires node-rest-client and async modules
$ npm install node-rest-client $ npm install async
In this example I used the REST API of IBM sterling B2B Integrator to create 50 SFG trading Partners.
You can use this Node.js code with any other REST API. The JSON Input data and the Web service Uri need to be adjusted.
createMultiplePartners.js
var Client = require('node-rest-client').Client; var async = require('async'); //TODO: Change var address = "http://0.0.0.0"; var port = "5074" var apiUri = "/B2BAPIs/svc/" var baseUri = address + ":" + port + apiUri; //TODO: Change var auth_info = {user:"ApiUser", password:"********"}; //Create client instance with auth info client = new Client(auth_info); //Define required headers var headers = { "Content-Type": "application/json", "Accept": "application/json" }; //The community used here called SFG should Exist //Example provided here //https://sterlingsync.com/sample-node-js-rest-api-client-for-ibm-sterling-b2b-integrator/ var jsonData= { "addressLine1": null, "addressLine2": null, "appendSuffixToUsername": false, "asciiArmor": false, "authenticationHost": null, "authenticationType": "External", "authorizedUserKeyName": null, "city": null, "code": null, "community": "SFG", "consumerCdConfiguration": null, "consumerFtpConfiguration": null, "consumerFtpsConfiguration": null, "consumerSshConfiguration": null, "consumerWsConfiguration": null, "countryOrRegion": null, "customProtocolExtensions": null, "customProtocolName": null, "doesRequireCompressedData": false, "doesRequireEncryptedData": false, "doesRequireSignedData": false, "doesUseSSH": false, "emailAddress": "test@email.com", "givenName": "test", "isInitiatingConsumer": true, "isInitiatingProducer": false, "isListeningConsumer": false, "isListeningProducer": false, "keyEnabled": false, "mailbox": null, "partnerName": "myPartner0", "password": "password", "passwordPolicy": null, "phone": "00124205505", "pollingInterval": null, "postalCode": null, "producerCdConfiguration": null, "producerFtpConfiguration": null, "producerFtpsConfiguration": null, "producerSshConfiguration": null, "publicKeyID": null, "remoteFilePattern": null, "sessionTimeout": null, "stateOrProvince": null, "surname": "surname", "textMode": false, "timeZone": null, "useGlobalMailbox": false, "username": "myPartner0" } // set content-type header and data as json in args parameter var args = { data: jsonData, headers: headers }; var i = 1; var continueWhilst = true; async.whilst( function test() { return continueWhilst; }, createPartner, function (err) { } ); function createPartner(next) { client.post(baseUri + "tradingpartners/",args, function (data, response) { if(Buffer.isBuffer(data)){ data = data.toString('utf8'); } console.log(data); }); i += 1; //change json data args.data.partnerName='myPartner'+i; args.data.username='myPartner'+i; if (i === 50) { //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 createMultiplePartners.js
The users are created in Sterling B2B Integrator:
Leave a Reply