简体   繁体   English

在PowerShell中创建动态ReST请求主体

[英]Create a dynamic ReST request body in PowerShell

I am currently working on a use case for invoking ReST request in PowerShell. 我目前正在研究在PowerShell中调用ReST请求的用例。 The body of POST request is created dynamically, reading data from a CSV file. POST请求的主体是动态创建的,从CSV文件读取数据。

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. 注意:从示例中可以明显看出,请求正文包含几个特殊字符,例如@,$等。

This would be easier if you included your CSV, but basically, you can import the CSV as an array of objects, then convert that to JSON. 如果包含CSV,这会更容易,但是基本上,您可以将CSV作为对象数组导入,然后将其转换为JSON。

You can customize the objects that are created from importing the CSV by adding custom members so that the translation to JSON gives you the output you want. 您可以通过添加自定义成员来自定义从导入CSV创建的对象,以便JSON转换为您提供所需的输出。

You can also group or filter the array of objects to make different ones depending on certain conditions. 您还可以根据特定条件对对象数组进行分组或过滤,以制作不同的对象。

Here's a some sample code that probably won't work directly but should somewhat demonstrate the concept: 这是一些示例代码,可能无法直接使用,但应该可以稍微证明一下这个概念:

$json = Import-Csv -Path C:\my\data.csv |
    ForEach-Object -Process {
        $row = $_
        $propName = $row.Type.ToLower() + 'ConnectionId'
        $row | Add-Member -NotePropertyName $propName -NotePropertyValue $out["$mapping_name"].$name -Force -PassThru
    } |
    ConvertTo-Json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM