0

My curl call looks like

curl -v -H 'Content-Type:application/json' -d '{"instanceIds": "[i-081ec3ffa72eb338, i-0c7474fb67bb9043]", "region": "us-west-2a"}' http://localhost:3001/instances/delete 

and on my expressJS based server, the endpoint looks like

app.post('/instances/delete', function (req, res) {
  let {region, instanceIds} = req.body;
  console.log(region + "," + instanceIds);
  stopInstances(region, instanceIds)
    .then(function (data) {
      res.send(data);
    })
    .catch(function (reason) {
      res.status(500).send("Error in deleting instances: " + reason);
    });
});

The function stopInstances looks like

export let stopInstances = function (region, instanceIds) {    
  let params = {InstanceIds: instanceIds, DryRun: true};
  console.log(params);
  return ec2.stopInstances(params).promise();
};

However, the values are printed as string

InstanceIds: '[i-081ec3ffa72eb338, i-0c7474fb67bb9043]'  

How can I convert this into Array? I tried Array.from but that parses each character

Array.from('[i-081ec3ffa72eb338, i-0c7474fb67bb9043]')
(40) ["[", "i", "-", "0", "8", "1", "e", "c", "3", "f", "f", "a", "7", "2", "e", "b", "3", "3", "8", ",", " ", "i", "-", "0", "c", "7", "4", "7", "4", "f", "b", "6", "7", "b", "b", "9", "0", "4", "3", "]"]  

But I want [i-081ec3ffa72eb338, i-0c7474fb67bb9043]

Thanks

3
  • 2
    It's a string because you pass a string, you probably want {"instanceIds": ["i-081ec3ffa72eb338", "i-0c7474fb67bb9043"]... in your request. Commented Sep 12, 2017 at 22:19
  • Thank you for the sharp eyes! I am totally stupid Commented Sep 12, 2017 at 22:20
  • Feel free to delete the question Commented Sep 12, 2017 at 22:23

2 Answers 2

1

My curl call was wrong, based on comment from georg, I fixed it to

curl -v -H 'Content-Type:application/json' -d '{"instanceIds": ["i-081ec3ffa72eb338", "i-0c7474fb67bb9043"], "region": "us-west-2a"}' http://localhost:3001/instances/delete 

and that fixed it.

Sign up to request clarification or add additional context in comments.

1 Comment

Instead of answering, just delete. It is not useful for other people than you
0

You could split the string by comma

var parts = your_string.split(",")

Then clean the parts, I.e. Remove the braces

var cleaned = parts.map( function (part) { 

             return part.replace(/[]/g, "");

});

That cleaned variable should now have the parts you want.

Untested, and written from mobile, so you'll probably want to refactor the approach.

Comments

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.