I am currently working on a use case for invoking ReST request in PowerShell. The body of POST request is created dynamically, reading data from a CSV file.
Here is how my final request body should be
{
"@type": "mtTaskParameter",
"name": "$src_sfdc$",
"type": "EXTENDED_SOURCE",
"sourceConnectionId":"00002E0B00000000000C"
},
{
"@type": "mtTaskParameter",
"name": "$tgt_db_del$",
"type": "TARGET",
"targetConnectionId":"00002E0B00000000000D"
},
{
"@type": "mtTaskParameter",
"name": "$tgt_db_ups$",
"type": "TARGET",
"targetConnectionId":"00002E0B00000000000D"
},
{
"@type": "mtTaskParameter",
"name": "$tgt_status$",
"type": "TARGET",
"targetConnectionId":"00002E0B00000000000D"
}
}
Currently I have implemented like below
if($connectionParameterized -eq "true"){
$str = @"
"@type": "mtTaskParameter",
"name": "$name",
"type": "$type"
"@
if($type -eq "SOURCE"){
$sourceConnectionId = <get source id>
$str = $str+
@"
,"sourceConnectionId":"$sourceConnectionId"
"@
}
if($type -eq "TARGET"){
$targetConnectionId = <get target id>
$str = $str+
@"
,"targetConnectionId":"$targetConnectionId"
"@
}
$finalstr = $finalstr+@"
{
$str
},
"@
}
This works fine, but the code becomes really messy and so difficult to scale. Also while printing, the format is not proper.
Is there a better way to handle this?
Note: As evident from the example, the request body contains several special characters like @,$ etc.