我正在尝试使用Powershell Azure Table Storage API
执行Invoke-RestMethod
,但会返回 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& $滤波器=时间戳+ GE + datetime'2017-10-16T00:00:00 '+和+时间戳+了+ datetime'2017-10-17T00:00:00' 0字节有效载荷详细:收到内容类型的4841字节响应 应用/ JSON; charset = utf-8详细:POST https://telemaapimlog.table.core.windows.net/reporttable,带-1个字节 payload Invoke-RestMethod:リモートサーバーがエラーを返しました*:(415)不支持 媒体类型発生场所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) [调用-RestMethod],引发WebException + FullyQualifiedErrorId:WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
我在这个网站上已经阅读了一些类似的问题,但它们主要没有内容类型或json描述问题所以我认为我的代码没有问题。
有人有想法吗?请帮帮我。
答案 0 :(得分:3)
有趣的问题。您收到此错误的原因是您没有指定响应数据的格式,即Accept
请求标头的值。由于您尚未指定此值,因此存储服务将其值视为XML,而您指定的存储服务版本不支持该值。
一旦你包含了这个Accept
标题(并在$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