Jump to content
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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...