【发布时间】:2021-05-02 09:09:41
【问题描述】:
我正在尝试编写一些 PowerShell 来更新我的 Cloudflare 帐户上的“A”记录。代码如下所示:
##Configure headers
$Bearer = "Bearer " + $cloudflareAPIToken
$Headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$Headers.Add("Authorization",$Bearer)
##Configure Payload
$Body = @{
type = 'A'
Name = 'homepc'
Content = '8.8.8.8'
ttl = 1}
$Jsonbody = $Body | ConvertTo-Json -Compress
Invoke-RestMethod -Method PUT "https://api.cloudflare.com/client/v4/zones/$DNSZoneID/dns_records/$ARecordID" -Headers $Headers -Body -$Jsonbody -ContentType 'application/json'
我得到了错误
Invoke-RestMethod: {"result":null,"success":false,"errors":[{"code":9207,"message":"Content-type must be application/json."}],"messages":[]}
我的 json 负载看起来像这样
PS C:\Users\Frank> $Jsonbody
{"type":"A","Name":"homepc","ttl":1,"Content":"8.8.8.8"}
如果我将此有效负载剪切并粘贴到 Postman 中,那么它可以工作。 Bellow 是 Postman 生成的代码。这行得通
curl --location --request PUT 'https://api.cloudflare.com/client/v4/zones/6b5e4cf4634bc20cebc1cd96072c/dns_records/4cb90f1dabfcc63a97055234520' \
--header 'Authorization: Bearer zHl3LhnQzRyw1_' \
--header 'Content-Type: application/json' \
--data-raw '{"type":"A","Name":"homepc","Content":"8.8.8.8","ttl":1}'
谁能帮我弄清楚这个问题?
提前致谢
弗兰克
【问题讨论】:
标签: json powershell api cloudflare rest