1

遵循许多其他人建议的脚本(从这里开始)可以正常工作,但我遇到了一个超出我理解能力的错误。我是 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

Get API的 Go Daddy 参考页面在此处,Put API 的参考页面在此处

4

1 回答 1

2

PUT API 文档说它期望 body 是一个数组。这也是错误消息所说的。尝试更改此行:

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

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

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

@{} 在 PowerShell 中创建一个哈希表,当转换为 JSON 时,它将是一个对象

于 2019-04-05T20:59:29.573 回答