Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

like image 435
Falcones Avatar asked Dec 05 '14 22:12

Falcones


1 Answers

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.

like image 177
briantist Avatar answered Sep 18 '22 17:09

briantist