Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke-RestMethod : The operation has timed out

I have a script that deletes resources and creates new ones. Every now and then it times out and I can't figure out why. It seems to happen when I run the script multiple times, but I couldn't get a definite pattern. I found that my server hasn't picked up the message yet since there's no logging for the request yet.

$old_values = Invoke-RestMethod -Uri $uri -Method Get

foreach($old_value in $old_values.result) {
    Invoke-RestMethod -Uri "$uri&key=$old_value.id" -Method Delete
}

$new_value = Invoke-RestMethod -Uri "$uri" -Body "{}" -ContentType application/json -Method Post

Interesting note, I get occasional timeouts when I run the Invoke-RestMethod calls directly from powershell. I also ran them with Fiddler and never got timeouts.

[Edit] I've been checking the connections with netstat. While the commands are hanging, they're listed as ESTABLISHED. But I keep seeing TIME_WAIT connections listed to my server. Is there a chance my connections aren't getting closed?

like image 735
chani Avatar asked Apr 29 '14 01:04

chani


1 Answers

The link David Brabant posted above contains this workaround that solved the problem for me:

$r = (Invoke-WebRequest -Uri $uri `
    -Method 'POST' `
    -ContentType 'application/json' `
    -Body '{}' `
    ).Content | ConvertFrom-Json
like image 69
Allanrbo Avatar answered Nov 18 '22 19:11

Allanrbo