2

I'm trying to write a program that deletes files from a specific device. The device has a REST API and I can access it from the CLI without any problems.

For example if I write this in the CLI, it works :

$clip="test.mov"
$ClipsURL="http://123.45.67.89/clips"
$ClipToDelete=@{action="delete";clipname=$clip}
Invoke-RestMethod -Uri $ClipsURL -Body $ClipToDelete -Method Post -ContentType "application/x-www-form-urlencoded"

I can play around with the $ClipToDelete parameter (changing the value of $clip) and it works every time.

Now when I put that in a loop (in my main script) it works the first time, and times out next.

foreach($clip in $ListClips) {
$clip="test.mov"
$ClipsURL="http://123.45.67.89/clips"
$ClipToDelete=@{action="delete";clipname=$clip}
Invoke-RestMethod -Uri $ClipsURL -Body $ClipToDelete -Method Post -ContentType "application/x-www-form-urlencoded"
}

While debuging I can clearly see that all the values passed as parameters to Invoke-RestMethod are correct (the URL & Body are correct).

My first impression is that I should probably close the session (if that makes any sense) before trying to Post again.

I tried adding a SessionVariable parameter to the command but it didnt change a thing

Does anyone already know how to close a web sesion left open (with the new Invoke-RestMethod command) ? Or does anyone think that the issue lies elsewhere ?

Thank you.

2 Answers 2

1

I had faced similar issues while invoking REST APIs from AirWatch. Evidently the bug has been filed for POST & DELETE methods below:

https://connect.microsoft.com/PowerShell/feedback/details/836732/tcp-connection-hanging-in-close-wait-when-using-invoke-restmethod-with-put-or-delete

But I did face this issue intermittently with GET requests too, I documented the workarounds I had to use in the below TechNet Wiki article, Maybe you can try those and improve it if you find something extra :

http://social.technet.microsoft.com/wiki/contents/articles/29863.powershell-rest-api-invoke-restmethod-gotcha.aspx

0
foreach($clip in $ListClips) {
$clip="test.mov"
$ClipsURL="http://123.45.67.89/clips"
$ClipToDelete=@{action="delete";clipname=$clip}
Invoke-RestMethod -Uri $ClipsURL -Body $ClipToDelete -Method Post -ContentType "application/x-www-form-urlencoded"
}

In the second line you are overriding the current value with "text.mov".

If it's not a typo error, it could be that you delete the object the first time and you receive timeouts when trying to delete it again (it depends of course on the server-side implementation)

1
  • It most likely is due to the server-side implementation. I'll try and contact the manufacturer of the device (it's an HD-SDI recorder : AJA Ki Pro). Thanks for the answer
    – Zakari
    Commented Jul 15, 2013 at 16:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.