5
votes

I'm trying to create a powershell script to access DYN's API and perform checks/updates on DNS zones I use/test.

I'm following their API details and here's the first link, https://help.dyn.com/session-log-in/

Here's the beginning of the REST script I've put together:

$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. 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

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:

$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. Any help would be very much appreciated at this point.

-Sean

1
Did you ever find an answer on this? I'm having similar issues.Jason Carter
I was able to get the session token and then work within the DNS structure, but I ran into problems with subzones and other things. I was not able to get the full zone creation/mgmt functional, due to the problems with their URL-based command structure. I may still be able to recreate what I built, or if you have a sample I can take a look to see what you have in there.Sean Falconer

1 Answers

14
votes

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.

According to the documentation on 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". Otherwise, the content type is not specified in the call.

ConvertTo-JSON

You don't have to create your JSON string manually. 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.