Reputation: 92179
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
Upvotes: 0
Views: 159
Reputation: 6532
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.
Upvotes: 0
Reputation: 92179
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.
Upvotes: 1