Dotty
Dotty

Reputation: 55

Forcing UTF-8 on powershell during invoke-webrequest

Im trying to use "invoke-webrequest" CMDLET for callin a web service. Im successfully using it with WSDL served by apache tomcat,, but when I use it against glassfish 2 I get:

---Invoke-WebRequest : '"utf-8"' is not a supported encoding name.---

My request works in SOAP UI just well, but using SOAP UI I realize Glassfish V2 is setting the UTF-8 encoding with double quotes around (see the error above and will give you the clue). Apache Tomcat does it just well, it gives me the encoding without the double quotes.

How do you deal with this? is there any way to remove those double quotes dynamically or maybe replace the encoding altogether during the CMDLET call? I could find any reference to this on the documentation.

Thanks!

Upvotes: 0

Views: 6492

Answers (1)

Keith Hill
Keith Hill

Reputation: 201632

Invoke-WebRequest is just setting up a plain old HTTP header/body request for you. Are you sure you're creating the correct envelope with header and body with all the correct XML namespaces etc? I'm not saying you can't do this but in general, it might be easier to use New-WebServiceProxy to create a proxy against the WSDL for the service.

If you still want to go this way, just pass in a hashtable with all the headers you want to use, including the one that sets the charset e.g.:

Invoke-WebRequest ... -Headers @{Host = 'www.xxxx.xxx';
                                 Content-Type = 'application/soap+xml; charset=utf-8';
                                 Content-Length = <length>;
                                 SOAPAction = 'http://www.w3.org/2003/05/soap-envelope;
                                 ...}

Upvotes: 1

Related Questions