Jump to content
Monthly Subscriptions Are Here Read more... ×
Matt

API and Powershell

Recommended Posts

Matt

I'm trying to build an interface between Domotz and our documentation program marrying the information in SQL Server.  Any ideas for getting the connection to the API working properly?  This is definitely not connecting since I'm seeing no incremental access to the API key in the portal.  TIA!

 

$conn = New-Object System.Data.SqlClient.SqlConnection("Server=SERVER;Database=Information;Integrated Security=True") 
$conn.Open() 
$cmd = $conn.CreateCommand() 
$Uri = 'https://api-us-east-1-cell-1.domotz.com/public-api/v1/agent' 
$headers = @{
    'Accept' = 'application/json'
    'X-Api-Key' = 'XXXXXX'
}
$result = Invoke-WebRequest -Uri $Uri -Headers $headers -Method Get

 

 

Yields the following:

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:11
+ $result = Invoke-WebRequest -Uri $Uri -Headers $headers -Method Get
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Share this post


Link to post
Share on other sites
Giancarlo

I'm not an expert on Powershell, but reading the code it seems there might be an issue in the headers definition, since there is no delimiter between the two definitions:

 

$headers = @{
    'Accept' = 'application/json'  <<--- Missing something?
    'X-Api-Key' = 'XXXXXX'
}

Please, try with the following approach to add the headers:

$headers = @{}
$headers.add("Accept","application/json")
$headers.add("X-Api-Key","XXXXXX")

source:

 

https://stackoverflow.com/questions/12936150/is-it-possible-to-send-additional-http-headers-to-web-services-via-new-webservic

 

Let me know if you succeed.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×