Powershell Invoke RestMethod error 415 Unsupported Media Type
Interesting problem. The reason you're getting this error is because you have not specified the format of response data i.e value for Accept
request header. Because you have not specified this value, storage service treats its value as XML which is not supported for the storage service version that you have specified.
Once you include this Accept
header (and specify value for PartitionKey and RowKey in your $body
), things should work just fine.
Here's the code that I wrote:
$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