Forum Discussion
Hi Nick,
This one is a bit tricky - we didn't publish much around this resource because there wasn't a lot of interest and also it starts to overlap with where the LogicModule Exchange is useful. That being said, we'll update the docs to provide more useful examples. We previously only supported Basic Auth for this resource, but have since added support for authenticating via API Tokens. In order to get the token authentication to work, you need the following:
- (1) the payload needs to include the xml file content enclosed in boundaries (can be random)
- (2) the content type needs to be set to multipart/form-data and it needs to call out the boundaries
And in your code above, you'll want the /importxml to be tacked on to the resource path (I recognize that doesn't align with the proper REST resource naming, and we'll improve it in a future release). So a successful example in PowerShell looks like this:
<# account info #>
$accessId = '48v2wRzfK94y53sq5EuF'
$accessKey = 'aB=k9opY87U~^_cvHiq~98G-j6Q_ja4sr6J8-4+)'
$company = 'api'
<#File & Request info#>
$uploadFile = 'test.xml'
$FilePath = "C:\Users\Sarah\Desktop\test.xml"
$httpVerb = 'POST'
$resourcePath = '/setting/datasources/importxml'
$queryParams = ''
$file = Get-Content $FilePath
$boundary = [System.Guid]::NewGuid().ToString()
$data = '------$boundary
Content-Disposition: form-data; name="file"; filename="test.xml"
Content-Type: text/xml
'+$file+'
------$boundary--'
<# Construct URL #>
$url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath + $queryParams
<# Get current time in milliseconds #>
$epoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end (Get-Date).ToUniversalTime()).TotalMilliseconds)
<# Concatenate Request Details #>
$requestVars = $httpVerb + $epoch + $data + $resourcePath
<# Construct Signature #>
$hmac = New-Object System.Security.Cryptography.HMACSHA256
$hmac.Key = [Text.Encoding]::UTF8.GetBytes($accessKey)
$signatureBytes = $hmac.ComputeHash([Text.Encoding]::UTF8.GetBytes($requestVars))
$signatureHex = [System.BitConverter]::ToString($signatureBytes) -replace '-'
$signature = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($signatureHex.ToLower()))
<# Construct Headers #>
$auth = 'LMv1 ' + $accessId + ':' + $signature + ':' + $epoch
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization",$auth)
$headers.Add("Content-Type",'multipart/form-data; boundary=----$boundary')
<# Make Request #>
$response = Invoke-RestMethod -Uri $url -Method $httpVerb -Body $data -Header $headers
<# Print status and body of response #>
$status = $response.status
$body = $response.data
Write-Host "Status:$status"
Write-Host "Response:$body"
Let me know if this doesn't work for you, or if you have additional issues/feedback.
Thanks!
Sarah
Related Content
- 5 months ago
- 3 months ago
- 7 months ago