[ How To ] call the REST API from Windows PowerShell

While troubleshooting an issue, I had to figure out how to get information from the REST API on a Windows 10 host. There are several ways to achieve this, I wanted to use only already installed tools.

All that was needed is PowerShell. Let’s say we want to get the configuration of Syncthing. Here’s a small guide :

1.open your Syncthing GUI in a web browser, leave it open.

2.open powershell, click Start button and type Powershell.

3.copy & paste this into the shell : $Url = "http://localhost:8384/rest/system/config" .Replace 8384 with whatever portnumber you used to open the GUI in step 1. You can type $url in the shell, and compare the output to what you pasted.

4.Paste this into the shell : $Headers = @{"X-API-KEY" = ""} but do not hit enter yet, we need to add your API key to this command before executing it.

5.go to the browser window / GUI, click on Actions/Settings. You will see your API Key; it will look similar to this : hnxk02c3zQi5EWNLRcQ7w2HSRY46gjAQ, select it and copy it.

6.back to the shell, paste your API key in between the double quotes, so that the command looks similar to $Headers = @{"X-API-KEY" = "hnxk02c3zQi5EWNLRcQ7w2HSRY46gjAQ"}. Hit Enter

7.check your work , type $Headers in the shell, it will output something similar to

Name                           Value

X-API-KEY hnxk02c3zQi5EWNLRcQ7w2HSRY46gjAQ

8.paste this into the shell : Invoke-RestMethod -Uri $Url -Headers $Headers -Method Get -Outfile response.xml and open the file response.xml with a text editor. If you don’t know the location of your response.xml file, just type pwd in the shell.

9.try another API call, for instance $Url = "http://localhost:8384/rest/db/file?folder=yourfolder&file=yourfile", will show you some info about a fille called yourfile in the shared folder named yourfolder. After you change the $Url variable, issue the command from step8 again.

6 Likes

Sorry for reviving, sonce this method is still valid & actively being referred to, I’d like to add this but I can not edit the OP.

Troubleshooting :

The Invoke-RestMethod cmdlet was introduced in Windows PowerShell 3.0.

Check your Powershell version if you get an error indicating something like : The term 'Invoke-RestMethod' is not recognized as the name of a cmdlet, function, script file, or operable program.

2 Likes