Sea*_*ner 4 api rest powershell
我正在尝试创建一个powershell脚本来访问DYN的API并对我使用/测试的DNS区域执行检查/更新.
我正在关注他们的API详细信息,这是第一个链接,https://help.dyn.com/session-log-in/
这是我编写的REST脚本的开头:
$url = "https://api2.dynect.net/REST/Session/"
$body = @{customer_name='mahcompany';user_name='mahname';password='mahpass'}
Invoke-RestMethod -Method Post -Uri $url -Body $body
Run Code Online (Sandbox Code Playgroud)
这会产生以下结果:
Invoke-RestMethod:远程服务器返回错误:(406)Not Acceptable.在行:12 char:9 + $ test = Invoke-RestMethod -Method Post -Uri $ url -Body $ body + ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidOperation :( System.Net.HttpWebRequest:HttpWebRequest)[Invoke-> RestMethod], WebException + FullyQualifiedErrorId:WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
根据DYN信息,这应该是一个JSON查询,所以我尝试了使用CURL作为基础的DYN的其他几个例子:
$json = @"{"customer_name":"yourcustomer","user_name":"youruser","password":"yourpass"}'
Run Code Online (Sandbox Code Playgroud)
然而,这也不起作用.
任何人都能指出我在正确的方向吗?这不是那么疯狂,我只是试图将参数传递给rest-method查询字符串.在这一点上,非常感谢任何帮助.
-Sean
bri*_*ist 12
Invoke-RestMethod -Method Post -Uri $url -Body $body -ContentType 'application/json'
Run Code Online (Sandbox Code Playgroud)
如果dyn.com期望正确的内容类型,这可能是问题.
根据以下文件Invoke-RestMethod
:
如果省略此参数且请求方法为POST,则Invoke-RestMethod将内容类型设置为"application/x-www-form-urlencoded".否则,调用中未指定内容类型.
ConvertTo-JSON
您不必手动创建JSON字符串.您可以创建一个哈希表,然后将其转换为:
$data = @{
customer = 'something'
name = 'whatever'
}
$data | ConvertTo-JSON
Run Code Online (Sandbox Code Playgroud)
我并不是说你肯定会制作格式错误的JSON,但这有助于防止这种情况发生.
归档时间: |
|
查看次数: |
14422 次 |
最近记录: |