Invoke-RESTMethod is my best friend under PoSH 3 🙂
I am querying a web service that either returns a hefty binary file or a simple text string of "No update available." I am building a script that, if it retrieves a file, needs to do other processing but should fall to a different branch should there not be an update file to work upon. While I can query the service and store the results in a variable which can then be string compared against "No update available," that means I may wind up storing the entire binary result in the variable. That's a big memory hit and, in some situations, may be too big to load up.
Is there a way to do a quick read of the URL and determine if the full binary file will be retrieved? I'm hoping there's an easier way than setting up webrequest streams and reading partial responses (that seems like a lot of work).
by DonJ at 2012-11-01 13:33:18
You're submitting a request to the URL; you have to take the entire response back. Now, you certainly don't have to store all of it. You could potentially just store enough characters to do your comparison, and if the compare fails, make another call to get the whole binary wodge. That's basically the streams approach you mention, and it's a good bit of code, yeah.
I'd probably just dump the response to a file (Invoke-RESTMethod has a parameter for doing so). It'd then be easy to check the file length and figure out if it contained "No update" or was a real file. That way I'm not storing anything in memory.
by davidski at 2012-11-02 21:04:45
I was hoping there was a clever way of getting to the underlying WebRequest method, but simply doing an -outfile and checking the output file length should do the trick as well. Thanks again, Don.