简体   繁体   English

Powershell Invoke-Webrequest w/ JSON Body - 无法反序列化……?

[英]Powershell Invoke-Webrequest w/ JSON Body - Can not deserialize…?

I need to perform an Invoke-Webrequest with a specifically formatted body to add devices to a product.我需要使用特定格式的主体执行 Invoke-Webrequest 以将设备添加到产品。 Here is what it looks like in json (example straight from the vendor's documentation):这是它在 json 中的样子(直接来自供应商文档的示例):

$body_json = '{"datasource": [{
            "parentId": "123456789000",
            "name": "(name)",
            "id": "(value)",
            "typeId": 0,
            "childEnabled": false,
            "childCount": 0,
            "childType": 0,
            "ipAddress": "(ipAddress)",
            "zoneId": 0,
            "url": "(url)",
            "enabled": false,
            "idmId": 123456789000,
            "parameters": [{
                "key": "(key)",
                "value": "(value)"
            }]
        }]}'

When I try to submit this in its json representation though, I get the following error:但是,当我尝试在其 json 表示中提交此文件时,出现以下错误:

Invoke-WebRequest : Can not deserialize instance of com.vendor.etc.DataSourceDetail out of START_ARRAY token at [Source: java.io.StringReader@22c614; Invoke-WebRequest :无法从 START_ARRAY 令牌中反序列化 com.vendor.etc.DataSourceDetail 的实例 [来源:java.io.StringReader@22c614; line: 1, column: 1] At C:\\powershell_script_location\\ps.ps1:114 char 9 + $request = Invoke-WebRequest $url -Method Post -Headers $headers -Body $body_json - ... +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand行:1,列:1] 在 C:\\powershell_script_location\\ps.ps1:114 char 9 + $request = Invoke-WebRequest $url -Method Post -Headers $headers -Body $body_json - ... +~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException +fullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

The issue is with the format of the "parameters", parameter because the request submits fine when omitting the "parameters", but then the devices that I'm adding are missing important parameter details.问题在于“参数”参数的格式,因为在省略“参数”时请求可以正常提交,但是我添加的设备缺少重要的参数详细信息。

Is there something wrong with Invoke-WebRequest, JavaScriptSerializer, the vendor's code, or is this a user error? Invoke-WebRequest, JavaScriptSerializer,供应商代码有问题,还是用户错误? Let me know if any clarification is needed.如果需要澄清,请告诉我。

Unfortunately I don't know what a com.vendor.etc.DataSourceDetail instance looks like, as I am using an API and I don't have access to it directly.不幸的是,我不知道com.vendor.etc.DataSourceDetail实例是什么样的,因为我使用的是 API,我无法直接访问它。

Use Invoke-RestMethod instead of Invoke-WebRequest .使用Invoke-RestMethod而不是Invoke-WebRequest

If you have the body as a string use:如果您将正文作为字符串使用:

Invoke-RestMethod -Uri http://your-url.com -Method POST -Body $body_json -ContentType "application/json"   

If the body must be constructed from data/parameters, it might be easier to build a hashtable and convert it to json via ConvertTo-Json :如果必须从数据/参数构造主体,则构建哈希表并通过ConvertTo-Json将其转换为 json 可能更容易:

$body_json = @{
    datasource = @(
        @{
            parentId = 123456789000
            name = "name"
            id = "value"
            typeId = 0
            childEnabled = $false
            childCount = 0
            childType = 0
            ipAddress = "ipAddress"
            zoneId = 0
            url = "url"
            enabled = $false
            idmId = 123456789000
            parameters = @( @{
                key = "key"
                value = "value"
            })
       })} | ConvertTo-Json -Depth 4

Invoke-RestMethod -Method 'Post' -Uri http://your-url.com -Body $body_json -ContentType "application/json"

Body Undefined身体未定义

I couldn't understand why the req.body on the server was undefined (NodeJS Azure Function).我不明白为什么服务器上的req.body undefined (NodeJS Azure 函数)。 It turns out I had a header that was an empty string .原来我有一个空字符串标头

It's not clear whether is was invoke-restmethod or azure-functions-core-tools that has a bug.目前尚不清楚是invoke-restmethod还是azure-functions-core-tools存在错误。

FWIW. FWIW。

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

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