简体   繁体   English

Powershell Invoke-RestMethod

[英]Powershell Invoke-RestMethod

I'm trying to create a powershell script to access DYN's API and perform checks/updates on DNS zones I use/test. 我正在尝试创建一个powershell脚本来访问DYN的API并对我使用/测试的DNS区域执行检查/更新。

I'm following their API details and here's the first link, https://help.dyn.com/session-log-in/ 我正在关注他们的API详细信息,这是第一个链接, https://help.dyn.com/session-log-in/

Here's the beginning of the REST script I've put together: 这是我编写的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

This produces the following results: 这会产生以下结果:

Invoke-RestMethod : The remote server returned an error: (406) Not Acceptable. Invoke-RestMethod:远程服务器返回错误:(406)Not Acceptable。 At line: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 在行: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

This is supposed to be a JSON query according to the DYN information, and so I've tried sevveral other examples of DYN's using CURL as a basis: 根据DYN信息,这应该是一个JSON查询,所以我尝试了使用CURL作为基础的DYN的其他几个例子:

$json = @"{"customer_name":"yourcustomer","user_name":"youruser","password":"yourpass"}' 

However this doesn't work either. 然而,这也不起作用。

Can anyone point me in the right direction here? 任何人都能指出我在正确的方向吗? This can't be that crazy, I'm just trying to pass the parameters into a rest-method query string. 这不是那么疯狂,我只是试图将参数传递给rest-method查询字符串。 Any help would be very much appreciated at this point. 在这一点上,非常感谢任何帮助。

-Sean -Sean

Content Type 内容类型

Invoke-RestMethod -Method Post -Uri $url -Body $body -ContentType 'application/json'

This might be the problem if dyn.com is expecting a proper content type. 如果dyn.com期望正确的内容类型,这可能是问题。

According to the documentation on Invoke-RestMethod : 根据Invoke-RestMethod上的文档:

If this parameter is omitted and the request method is POST, Invoke-RestMethod sets the content type to "application/x-www-form-urlencoded". 如果省略此参数且请求方法为POST,则Invoke-RestMethod将内容类型设置为“application / x-www-form-urlencoded”。 Otherwise, the content type is not specified in the call. 否则,调用中未指定内容类型。

ConvertTo-JSON

You don't have to create your JSON string manually. 您不必手动创建JSON字符串。 You can create a hashtable and then convert it: 您可以创建一个哈希表,然后将其转换为:

$data = @{
    customer = 'something'
    name = 'whatever'
}
$data | ConvertTo-JSON

I'm not saying that you are definitely making malformed JSON, but this can help prevent that. 我并不是说你肯定会制作格式错误的JSON,但这有助于防止这种情况发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM