INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Powershell Invoke-Restmethod and Invoke-WebRequest fail with MCWS (Resolved)  (Read 6702 times)

terrym@tassie

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 504

I was playing around with Powershell and MCWS and came across the following problem;
Invoke-Restmethod and Invoke-WebRequest both fail with the following error when accessing the JRiver web service MCWS
Code: [Select]
Invoke-RestMethod http://localhost:52199/MCWS/v1/alive -Verbose
VERBOSE: GET http://localhost:52199/MCWS/v1/alive with 0-byte payload
VERBOSE: received 385-byte response of content type text/xml ; charset="utf-8"
Invoke-RestMethod : Value cannot be null.
Parameter name: name
At line:1 char:1
+ Invoke-RestMethod http://localhost:52199/MCWS/v1/alive -Verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], ArgumentNullException
    + FullyQualifiedErrorId : System.ArgumentNullException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
 

The error returned is extremely opaque but fortunately I found an explanation here http://stackoverflow.com/questions/36275618/why-is-invoke-webrequest-and-invoke-restmethod-failing-and-succeeding-at-the-sam

As is explained on stackoverflow the error is caused because MCWS is returning a content header with the charset set as charset="utf-8" (quotes) as opposed to the more normal charset=utf-8 (no quotes)

Here is an example of Powershell working with a REST api that returns a content header with charset=utf-8 (no quotes)
Code: [Select]
Invoke-RestMethod $url -Verbose
VERBOSE: GET http://ibl.api.bbci.co.uk/ibl/v1/episodes/p03r3y5p?rights=web&availability=all with 0-byte payload
VERBOSE: received 2146-byte response of content type application/json; charset=utf-8

version schema                  episodes                                                                                                                    
------- ------                  --------                                                                                                                    
1.0     /ibl/v1/schema/ibl.json {@{id=p03r3y5p; live=False; type=episode_large; (truncated response for clarity)


Now as noted on stackoverflow this is pretty sloppy from Microsoft but it leaves the problem that the JRiver MCWS api isn't accessible using standard methods from the standard Microsoft scripting language.

Is there a particular reason that the charset has been quoted and set this way in the MCWS content header?

Of course this issue can be worked around, I am using the following:
Code: [Select]
$mc_xml=New-Object XML
$mc_xml.Load("http://localhost:52199/MCWS/v1/alive")
But this is not the "normal" method to access a REST api in Powershell.

If anyone has has any other solutions for accessing MCWS using Powershell, I'd be grateful if they would share them.
Thanks,

Terry
Logged
Good manners cost nothing.
Bad manners can cost you your reputation
― B.D.Hawkey

Hendrik

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 10935
Re: Powershell Invoke-Restmethod and Invoke-WebRequest fail with MCWS
« Reply #1 on: May 06, 2016, 04:45:31 am »

Both are equally valid HTTP, and it appears UPNP devices generally use the quoted variant, so it was probably used to avoid a conflict with one of them, which works the opposite way as the Microsoft stuff. Oh well.
I wonder if we can remove it for MCWS only and leave it for UPNP/DLNA.
Logged
~ nevcairiel
~ Author of LAV Filters

Hendrik

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 10935
Re: Powershell Invoke-Restmethod and Invoke-WebRequest fail with MCWS
« Reply #2 on: May 06, 2016, 04:54:32 am »

We'll try this in the next build, should hopefully only impact MCWS and not DLNA/UPNP:
Changed: MCWS responses use a more standard Content-Type header to improve compatibility with some web service clients.
Logged
~ nevcairiel
~ Author of LAV Filters

terrym@tassie

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 504
Re: Powershell Invoke-Restmethod and Invoke-WebRequest fail with MCWS
« Reply #3 on: May 06, 2016, 02:33:15 pm »

Hendrik, thanks very much for the quick response.
As you say both methods are compliant with the RFCs, it is Microsoft that are non-compliant.

However I am pleased you are prepared to make the change so promptly, can't imagine Microsoft being so accommodating!

Lets hope it doesn't cause issues for other clients.
Logged
Good manners cost nothing.
Bad manners can cost you your reputation
― B.D.Hawkey

terrym@tassie

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 504
Re: Powershell Invoke-Restmethod and Invoke-WebRequest fail with MCWS
« Reply #4 on: May 09, 2016, 09:44:50 pm »

Confirming resolution in MC 21.0.80
Code: [Select]
Invoke-RestMethod http://localhost:52199/MCWS/v1/alive -Verbose
VERBOSE: GET http://localhost:52199/MCWS/v1/alive with 0-byte payload
VERBOSE: received 385-byte response of content type text/xml;charset=utf-8

xml                                             Response
---                                             --------
version="1.0" encoding="UTF-8" standalone="yes" Response

Thanks again for such a prompt fix.

PS Just upgraded my master licence to MC22 to show my appreciation (plus I know a good deal when I see one) :)
Logged
Good manners cost nothing.
Bad manners can cost you your reputation
― B.D.Hawkey
Pages: [1]   Go Up