0

I have a long-running post request to a rest endpoint which might take a few minutes to finish and I need the response from it to start another

I have tried using Invoke-RestMethod and Invoke-WebRequest but both seem to not be able to track the request after it has finished unlike when I send the request from something else other than PowerShell. What is happening is the script looks like its waiting for something but it doesn't matter howling I wait for nothing comes back!

All I found was a suggestion to include -DisableKeepAlive but that didn't help even though the documentation seems to be the opposite of what I need!

I did get a response after a long time but only happened once! using the following

Invoke-RestMethod -Method post -DisableKeepAlive -ContentType 'Application/Json' -Uri https://localhost/api/import

Any suggestions? is this even possible? Or do I need to take a different approach?

et3rnal
  • 322
  • 4
  • 17
  • What about `-TimeoutSec` ? Did you try cranking that up ? – Sage Pourpre Nov 15 '21 at 11:49
  • Hmmm, looks like the default value is 0 / indefinite already... But maybe it's worth trying it anyway. – Sage Pourpre Nov 15 '21 at 11:50
  • 2
    Here is someone else that reference this exact thing (aka that it should be indefinite but default to 100 seconds) : https://stackoverflow.com/a/27013279/934946 Setting `-TimeoutSec` to something higher than 100 second might resolve your issue. – Sage Pourpre Nov 15 '21 at 11:53
  • `-TimeoutSec` will just make it throw an exception after the specified time, I need the result to come back which is now. Or I'm missing something? – et3rnal Nov 15 '21 at 23:53
  • It will make an exception after the timeout time is elapsed, but it will also wait up to that point. I thought you had issues because it wasn't waiting long enough. If so, put the timeout something higher, let's say 10 minutes. If you expect your request to take up to 5 min., then you should never see the timeout exception, unless there is actuallly an issue and it reach the 10 min, in which case you'll have to decide what to do about it. – Sage Pourpre Nov 16 '21 at 00:07
  • I see, I'll try that. BTW I waited for 30 minutes and I finally got the result for the second time! however, the request doesn't need that time it takes about 7 minutes. – et3rnal Nov 16 '21 at 00:27
  • Are you sure that your API consistently return in 10 minutes or less ? Do you have logs that can confirm the timestamp of the response time ? I would be surprised that the API did indeed return in 7 min. and Powershell took an extra 23 minutes to show the result... The only thing I can think of is if the result returned was huge and you print the result in the console instead of storing it into a variable. That could account for some processing delay as there is a lot of overhead when printing in the console. If you think the issue is in the way Invoke-RestMethod / Webrequest work, you could – Sage Pourpre Nov 16 '21 at 02:06
  • You could try to build your own web request and send the data. Take a look at the answer on this thread which show the basic of creating / using a system.net.WebRequest https://social.technet.microsoft.com/Forums/scriptcenter/en-US/64c74e89-610e-4229-a56b-12f973232a0a/replace-invokerestmethod-in-powershell-20?forum=ITCG (If you do something similar but doing your post... then you could see if it still take 30 minutes or is way faster.) – Sage Pourpre Nov 16 '21 at 02:07
  • 1
    Timeout didn't change anything. You are right I think it tasks 7 minutes on the prod server. Locally its taking a lot longer. The result is just boolean. – et3rnal Nov 16 '21 at 03:07
  • Locally it seems to work and I get responses but never works in the prod server even though the prod server finishes the request in 7 minutes! the PS version on prod is `5.1.17763.22681` where locally it's `5.1.19041.1237` No idea if its the reason, I decided to introduce another endpoint to check the process which I call after the timeout and find out when it has finished. – et3rnal Nov 17 '21 at 02:12

0 Answers0