0

我正在尝试研究如何使用 PowerShell 作为客户端进行 OData 更新。我找到了用于测试的站点services.odata.org:http://services.odata.org/OData/OData.svc/$metadata

我试过这个:

Invoke-RestMethod -Method Put -ContentType 'application/json' `
    -Uri 'http://services.odata.org/V3/(S(k22mmq0ajlv45epd2psyysnd))/OData/OData.svc/Products(0)' `
    -Body ( @{ Description = 'CheesyPeas' } | ConvertTo-Json )

但我回来了

Invoke-RestMethod : <?xml version="1.0" encoding="utf-8"?>
  <m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <m:code />
    <m:message xml:lang="en-US">
      Error processing request stream. Type information must be specified for types that take part in inheritance.
    </m:message>
  </m:error>
At line:1 char:1
+ Invoke-RestMethod -Uri 'http://services.odata.org/V3/(S(k22mmq0ajlv45epd2psyysnd ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

我认为这与导航属性有关。理想情况下,我将针对没有导航属性的简单实体进行测试,直到我有一个基本的 PUT 工作但我找不到一个。谁能帮我搞定这个工作?

4

1 回答 1

3

正如错误所说,您需要指定您在正文中发送的类型。

编写如下代码:

Invoke-RestMethod -Method Put -ContentType 'application/json' `
    -Uri 'http://services.odata.org/V3/(S(k22mmq0ajlv45epd2psyysnd))/OData/OData.svc/Products(0)' `
    -Body ( @{ "odata.type" = 'ODataDemo.Product'; Description = 'CheesyPeas' } | ConvertTo-Json )
于 2014-06-18T02:05:02.913 回答