Go Daddy API的PowerShell Invoke-WebRequest错误

时间:2019-04-05 20:05:06

标签: api powershell godaddy-api

遵循许多其他人建议的脚本(from here)可以正常工作,我遇到的错误超出了我的理解范围。我是Power Shell的中级新手,只是从API入门。

脚本为:

$domain = 'example.com'                    # your domain
$name = 'xyz'                              # name of the A record to update
$key = 'myKey                              # key for godaddy developer API
$secret = 'mySecret'                       # Secret for godday developer API

$headers = @{}
$headers["Authorization"] = 'sso-key ' + $key + ':' + $secret
$result = Invoke-WebRequest https://api.godaddy.com/v1/domains/$domain/records/A/$name -method get -headers $headers
$content = ConvertFrom-Json $result.content
$dnsIp = $content.data

# Get public ip address
$currentIp = Invoke-RestMethod http://ipinfo.io/json | Select -exp ip

# THE CODE WORKS FINE UP TO HERE

if ( $currentIp -ne $dnsIp) {
    $Request = @{ttl=3600;data=$currentIp }
    $JSON = Convertto-Json $request

# THE FOLLOWING LINE FAILS WITH THE ERROR NOTED BELOW

    Invoke-WebRequest https://api.godaddy.com/v1/domains/$domain/records/A/$name -method put -headers $headers -Body $json -ContentType "application/json"
} 

对于最终的Invoke-WebRequest返回以下错误:

Invoke-WebRequest : {"code":"INVALID_BODY","fields":[{"code":"UNEXPECTED_TYPE","message":"is not a array","path":"records"}],"message":"Request body doesn't fulfill schema, see details in `fields`"}
At C:\tfsCode\tfs\api.ps1:25 char:5
+     Invoke-WebRequest https://api.godaddy.com/v1/domains/$domain/reco ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

the Get API is herethe Put API is here的Go Daddy参考页。

1 个答案:

答案 0 :(得分:2)

PUT API文档说它期望主体是一个数组。这也是错误消息在说什么。尝试更改此行:

$Request = @{ttl=3600;data=$currentIp }

$Request = @(@{ttl=3600;data=$currentIp })

@()在PowerShell中创建一个数组,当转换为JSON时,它将仍然是数组

@ {}在PowerShell中创建哈希表,将其转换为JSON后将成为对象