Invoke-Webrequest incorect Json格式“无法绑定参数'标题'”

时间:2018-02-13 16:52:48

标签: json powershell azure header

我正在尝试向Azure WebHook发送包含标题+ Body和WebRequest的Body:

$Body = @"
{
    "headers":
    {
        "ocp-apim-subscription-key":"xxxxxx",
        "ocp-apim-trace":"true"
    },
    "Body":
    {
        "toto": "xxxxxx-1505-xxxxx-8113-xxxxxxxxxx",
        "status": "Enable",
    }
}"@

我正在使用:

Invoke-RestMethod -Uri $ webhook_url -Headers $ webhook_headers -Method $ webhook_method -Body $ Body

一切正常,在Azure之后,我收到Params输入:

workflow TestAPI
{
Param ([object]$WebHookData)
    if ($WebHookData)
    {
           # Get Webhook data
        $WebhookName = $WebHookData.WebhookName
        $WebhookHeaders = $WebHookData.RequestHeader
        $WebhookBody = $WebHookData.RequestBody

           # Body converting
        $body = (ConvertFrom-Json -InputObject $WebhookBody)

在我的脚本结束时,我将它再次转换为Json并使用Invoke-Webrequest:

    $body_json = (Convertto-Json -InputObject $body.body -Depth 10)
    $header_json = (Convertto-Json -InputObject $body.headers -Depth 10 )
$response = Invoke-RestMethod -Uri $URI_key -Method $Method -Body $body_json -Headers $header_json -ContentType "application/json" -UseBasicParsing

但是我收到了这个错误:

Invoke-RestMethod:无法绑定参数'Headers'。无法转换“{     “ocp-apim-subscription-key”:“xxxxxxxxxxx”,     “ocp-apim-trace”:“true” “类型”System.String“的值,键入”System.Collections.IDictionary“。

这是ConvertFrom-Json之后PsObject中值$ body.headers的输出:

@{ocp-apim-subscription-key=xxxxxxxxx; ocp-apim-trace=true}

在ConvertTo-Json之后输出$ header_json:

{
    "ocp-apim-subscription-key":  "xxxxxxxxxxxxx",
    "ocp-apim-trace":  "true"
}

2 个答案:

答案 0 :(得分:1)

要添加到Persistent13的命令中,您可以使用PowerShell这样简单的方法:

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1699)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:304)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:224)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1012)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:981)

答案 1 :(得分:0)

-Headers参数需要IDictionary个对象,json字符串不起作用。

您必须自己创建一个IDictionary对象,因为ConvertFrom-Json会将json反序列化为PSCutomObject

你可以这样做:

$headers = @{}
($body | ConvertFrom-Json).Headers.PSObject.Properties | ForEach-Object { $headers[$_.Name] = $_.Value }

现在,您可以将$headers变量与Invoke-RestMethod一起使用。 这将解决您收到的错误。