Getting, editing and posting config with Powershell

Hi I’m trying to manage Syncthing with Powershell. As an exercise, I’m trying to turn on/off the master setting on all folders.

My code at the moment:

$Url = "http://localhost:8080/rest/system/config"

#Getting Config
$Config = Invoke-RestMethod -Uri $Url -Method Get

#Turning on Master on every folder
foreach ($folder in $Config.folders)
    {
        $folder.readOnly = "True"
    }

#curl headers from firefox developer console when a config is applied
$Headers = @{
#"Host" = "localhost:8080" 
#"User-Agent" = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0" 
#"Accept" = "application/json, text/plain, */*" 
#"Accept-Language" = "en-US,en;q=0.5"
#"Content-Type" = "application/json; charset=UTF-8" 
"X-CSRF-Token-UM5L5" = "JpdUOIqFIgPcGDQC0KAXqspLD0eHOvHq" 
#"Referer" = "http://localhost:8080/" 
#"Content-Length" = "13507" 
"Cookie" = "CSRF-Token-UM5L5=JpdUOIqFIgPcGDQC0KAXqspLD0eHOvHq" 
#"Connection" = "keep-alive" 
#"Pragma" = "no-cache" 
#"Cache-Control" = "no-cache"
}

#Converting to Json and cleaning up
$JsonConfig = ($Config | ConvertTo-Json | Out-String).Replace(" ","")
$JsonConfig = $JsonConfig.Replace([Environment]::NewLine,"")

Invoke-RestMethod -Uri $Url -Method Post -Body $JsonConfig -Headers $Headers -ContentType application/json

It does not change any settings, and this error pops up: “Invoke-RestMethod : json: cannot unmarshal string into Go value of type []config.FolderDeviceConfigur ation”. But I get the same error in the web interface of Syncthing, so the posting part seems to work. I suspect the config isn’t formatted right before posting. I’ve tried without the cleanup at the end, and only using the ConvertTo-Json. Same error.

Anyone here who can help? :smile:

You should provide a dump of what you are posting. Are you saying the setting doesn’t work from the web ui?

1 Like

Good idea! I compared the config before and after editing and saw some differences. There were mainly (or only, can’t remember) 2 things the script did wrong. It did not convert the Object to Json deep enough, just the 2(default) layers. And Syncthing did not like “True”, but True (without the “”). I solved it by adding “-Depth 4”(maybe 3 is enough) and using the variable for true instead of “true”

The code could be cleaner, but at least it works :smile:

$Url = "http://localhost:8080/rest/system/config"

#Getting Config
$Config = Invoke-RestMethod -Uri $Url -Method Get

#Turning on Master on every folder
foreach ($folder in $Config.folders)
    {
        $folder.readOnly = $true
    }

#curl headers from firefox developer console when a config is applied
$Headers = @{
"X-CSRF-Token-UM5L5" = "JpdUOIqFIgPcGDQC0KAXqspLD0eHOvHq" 
"Cookie" = "CSRF-Token-UM5L5=JpdUOIqFIgPcGDQC0KAXqspLD0eHOvHq" 
}

#Converting to Json and cleaning up
$ConfigJson = $Config | ConvertTo-Json -Compress -Depth 4

Invoke-RestMethod -Uri $Url -Method Post -Body $ConfigJson -Headers $Headers -ContentType application/json

But I have a question about the headers. Do I need both? Can i substitute with the API key in some way?

You can specify the header X-API-Key and ignore the others.

1 Like

Thank you very much! :slight_smile: