2

I have an input in the format "ABC, DGE, GHI, ...."

I want to convert this comma-separated list into an array and then call my API to do a POST request for each value in the array. Can anyone give me a pointer on the best method?

Example

Call 1 POST data "caller" tens=ABC
Call 2 POST data "caller" tens=DFE
Call 3 POST data "caller" tens=GHI

working idea

var tens = "ABC, DEF, GHI";
var letters = tens.split(',');
const Array = callAPIs(letters) 

var data = {};

var caller_id = "caller";
data.caller_id = caller_id;

function callAPIs(letters) {
  var Array = [];
  for (let i = 0; i < letters.length; i++) {
   var apiRequest = http.request({
    'endpoint': 'http://xxxxxxxxxxxxxxxxxxxxxxx',
    'path':'/api/v1/table/record', 
    'method': 'POST',
    "headers": {
    "Authorization": "Basic xxxxxxxxxxxxxxxxxxxxxxxxxxx=",
    "Content-Type": "application/json"
    }
    }); 
8
  • You're already splitting on comma, so you just need to trim each string in the array to get rid of the white space. Commented Aug 12, 2020 at 18:08
  • Also what framework are you using? http.request() isn't a native javascript method as far as I am aware. Commented Aug 12, 2020 at 18:09
  • Sorry, i should od stated, I am using ECMAScript 6. Amended the title Commented Aug 12, 2020 at 18:14
  • @Taplar http.request() is Node. Commented Aug 12, 2020 at 18:16
  • @Iwrestledabearonce. aaaaaah, there we go Commented Aug 12, 2020 at 18:16

1 Answer 1

1

Is something like this what you expect?

var tens = "ABC, DEF, GHI"
var letters = tens.split(',').map(string=>string.trim())
const apiCalls = callAPIs(letters) 

function callAPIs(letters) {
  responses = []

  letters.forEach(group => {
    var apiRequest = http.request({
      'endpoint': 'http://xxxxxxxxxxxxxxxxxxxxxxx',
      'path':'/api/v1/table/record', 
      'method': 'POST',
      "headers": {
        "Authorization": "Basic xxxxxxxxxxxxxxxxxxxxxxxxxxx=",
        "Content-Type": "application/json"
      }
    })
    apiRequest.write(group)
    apiRequest.end((data) => {
      responses.push(data)
    })
  })
  
  return responses
}
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent! Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.