Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Powershell对http请求进行JSON对象格式化_Json_Powershell_Object - Fatal编程技术网

使用Powershell对http请求进行JSON对象格式化

使用Powershell对http请求进行JSON对象格式化,json,powershell,object,Json,Powershell,Object,我正在从事一个与Slack集成的项目,当我试图向API发送数据时遇到了一些问题,因为它需要格式化的块需要一个Json对象 使用Slack Block Builder,我知道它期望: "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": "http://bar.co

我正在从事一个与Slack集成的项目,当我试图向API发送数据时遇到了一些问题,因为它需要格式化的块需要一个Json对象

使用Slack Block Builder,我知道它期望:

    "blocks": [        
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "http://bar.com"
            }
        }
    ]
这就是我试图为
chat.postMessage
方法构造对象和参数以发送到API的方式

$messageText = 'http://bar.com'
$blocksDetailObj = [PSCustomObject]@{'type' = '"mrkdwn"'
                     'text' = $messageText}


$blocksObject = [PSCustomObject]@{ 'type' = '"section"'
                   'text' = $blocksDetailObj }

$blocksArray = @()

$blocksArray += $blocksObject

$queryStringParameters = @{ 'token' = $botToken 
                            'channel' = $channelID
                            'text' = 'fallback text'
                            'blocks' = $blocksArray 
                                     }
任何帮助或文档都将不胜感激。

您可能希望签出一个PS模块,以与Slack API交互,或者按照建议。你可以找到一篇很长的博客文章,上面给出了如何使用它的上下文

如果您不想直接使用这些模块,请查看:


您可能希望签出一个PS模块来与SlackAPI交互。或者PoshBot的回答是否有帮助?
$body = @{ }

    switch ($psboundparameters.keys)
    {
        'channel'     {$body.channel = $channel }
        'text'        {$body.text     = $text}
        'username'    {$body.username = $username}
        'asuser'      {$body.as_user = $AsUser}
        'iconurl'     {$body.icon_url = $iconurl}
        'iconemoji'   {$body.icon_emoji   = $iconemoji}
        'linknames'   {$body.link_names = 1}
        'parse'       {$body.parse = $Parse}
        'UnfurlLinks' {$body.unfurl_links = $UnfurlLinks}
        'UnfurlMedia' {$body.unfurl_media = $UnfurlMedia}
        'attachments' {$body.attachments = $Attachments}
    }
    $Messages += $Body
}
else
{
    foreach($Message in $SlackMessage)
    {
        $Messages += $SlackMessage
    }
}


foreach($Message in $Messages)
{
    if($Token -or ($Script:PSSlack.Token -and -not $Uri))
    {
        if($Message.attachments)
        {
            $Message.attachments = ConvertTo-Json -InputObject     @$Message.attachments) -Depth 6 -Compress
        }

        Write-Verbose "Send-SlackApi -Body $($Message | Format-List | Out-String)"
        $response = Send-SlackApi @ProxyParam -Method chat.postMessage -Body $Message -Token $Token -ForceVerbose:$ForceVerbose

        if ($response.ok)
        {
            $link = "$($Script:PSSlack.ArchiveUri)/$($response.channel)/p$($response.ts -replace '\.')"
            $response | Add-Member -MemberType NoteProperty -Name link -Value $link
        }

        $response
    }
    elseif($Uri -or $Script:PSSlack.Uri)
    {
        if(-not $ForceVerbose) {
            $ProxyParam.Add('Verbose', $False)
        }
        if($ForceVerbose) {
            $ProxyParam.Add('Verbose', $true)
        }
        $json = ConvertTo-Json -Depth 6 -Compress -InputObject $Message
        Invoke-RestMethod @ProxyParam -Method Post -Body $json -Uri $Uri
    }
    else
    {
        Throw 'No Uri or Token specified.  Specify a Uri or Token in the parameters or via Set-PSSlackConfig'
    }
}