Powershell 调用 RestMethod 错误 415 不支持的媒体类型

标签 powershell azure azure-table-storage invoke-command

我尝试使用 Powershell Invoke-RestMethod 执行 Azure Table Storage API,但它返回415 错误

这是 Powershell 代码:

$accessKey = "accesskey"
$account_name = "storage account"
$table_name = "table storage"
$date = "Fri, 10 Nov 2017 00:47:55 GMT"
$api_version = "2015-12-11"
$table_url = "https://$account_name.table.core.windows.net/$table_name"
$data_type = "application/json"
$signature = "generated signature"
$body = @{
Address="Mountain View";
Age="23"
}
$json = $body | Convertto-JSON
$table_headers = @{
"x-ms-date" = $date
"x-ms-version" = $api_version
"Authorization" = "SharedKey $account_name`:$signature"
"Content-Type" = $data_type
}

Invoke-RestMethod -Method POST -Uri $table_url -Headers $table_headers -Body $json -Verbose

远程服务器返回错误:

詳細: GET https://preprodtelemaapim.management.azure-api.net/reports/byApi?api-version=2017-03-01&$filter=timestamp+ge+datetime'2017-10-16T00:00:00'+and+timestamp+le+datetime'2017-10-17T00:00:00' with 0-byte payload 詳細: received 4841-byte response of content type application/json; charset=utf-8 詳細: POST https://telemaapimlog.table.core.windows.net/reporttable with -1-byte payload Invoke-RestMethod : リモート サーバーがエラーを返しました*: (415) Unsupported Media Type 発生場所 C:\Users\sasaki.hikaru\Desktop\repot.ps1:83 文字:1 + Invoke-RestMethod -Method POST -Uri $table_url -Headers $table_headers -Body $js ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod]、WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

我在这个网站上读到了一些类似的问题,但它们大多不是内容类型或 json 描述问题,所以我认为我的代码中没有问题。

有人有想法吗?请帮助我。

最佳答案

有趣的问题。您收到此错误的原因是您没有指定响应数据的格式,即 Accept 请求 header 的值。由于您尚未指定此值,存储服务会将其值视为 XML,您指定的存储服务版本不支持该值。

一旦包含此 Accept header (并在 $body 中指定 PartitionKey 和 RowKey 的值),一切就应该正常工作。

这是我编写的代码:

$accessKey = "account key"
$account_name = "account name"
$table_name = "table name"
$date = ([System.DateTime]::Now).ToString("R")
$api_version = "2016-05-31"
$table_url = "https://$account_name.table.core.windows.net/$table_name"
$data_type = "application/json"
$canonicalResource = "/$account_name/$table_name";
$stringToSign = "POST`n`n$data_type`n$date`n$canonicalResource";
$utf8enc = [System.Text.Encoding]::UTF8;
$bytes = $utf8enc.GetBytes($stringToSign)

$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [System.Convert]::FromBase64String($accessKey)
$signature = [System.Convert]::ToBase64String($hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($stringToSign)))


$body = @{
PartitionKey = "1";
RowKey = [System.guid]::NewGuid().ToString();
Address="Mountain View";
Age="23";
}

$json = $body | Convertto-JSON


$table_headers = @{
"x-ms-date" = $date
"x-ms-version" = $api_version
"Authorization" = "SharedKey $account_name`:$signature"
"Content-Type" = $data_type
"Accept" = "application/json;odata=fullmetadata"
}

Invoke-RestMethod -Method POST -Uri $table_url -Headers $table_headers -Body $json -Verbose

关于Powershell 调用 RestMethod 错误 415 不支持的媒体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47214227/

相关文章:

linux - 如何使用PowerShell命令Get-AzMetric获取Azure Linux VM guest 指标?

powershell - 如何抑制用户在没有参数参数的情况下调用我的 powershell 脚本时发生的错误

azure - 如何在 Azure 表存储中嵌入文档

javascript - Azure 表存储返回查询结果

azure - 在 Azure SAS 下允许多个 IP

c# - Windows Azure 表存储中的批量删除

visual-studio - 调试 PowerShell cmdlet 时出现问题

powershell - Powershell-如何基于文件名创建文件名数组?

visual-studio - 将 .NET Core 2.0 Web 应用程序发布到 Azure 应用服务时出错

azure - 将 NSG 添加到应用程序网关子网