Download Json file

Hi,

When i use https://icinga2.amg.local/director/importsource/fetch?id=31
i get a downloaded file like “director-importsource-31_20250109105643.json”

when i do this as a user every thing goes well !!
but i want to automated it with a powershell script ( Invoke-WebRequest) or somthing else , but is goes wrong.

i think the reason because the date is dynamic, how can i resove this ?

the code is


$credential = Get-Credential
$source = ‘https://icinga2/director/importsource/fetch?id=31
$destination = ‘.\export.json’
Invoke-WebRequest -Uri $source -OutFile $destination -Credential $credential

the result is an html file. not like the json i donwload by hand from the browser.

Hi @Ganz ,
I think you might be missing the Accept: application/json-Header.
Without that Icinga Web 2/Director might think you are a browser and send you some HTML.

This (more or less) documented.

1 Like

$credential = Get-Credential

$source = ‘https://icinga2/director/importsource/fetch?id=31

it’s the same output :frowning:

$source = ‘https://icinga2.amg.local/authentication/login?redirect=director%2Fimportsource%2Ffetch%3Fid%3D31
$destination = ‘.\export.txt’
Invoke-WebRequest -Uri $source -ContentType “application/json” -OutFile $destination -Credential $credential

Little not here: you can (and should) format stuff like this in code blocks, it is easier to read.
Could you try this:

Invoke-WebRequest  -Headers @{'Accept' = 'application/json'}

or something like this, powershell is not my preferred ecosystem.