Search code examples
powershellinvoke-webrequest

Invoke-WebRequest returns content as one array of Integers [System.Byte]


When I do a Invoke-WebRequest to a URI such as "http://SomeStoreName/npsharp/services/INP6Waycore/GetPosListXML" the response "Content" contains an array of strings.

The result of the invoke is

StatusCode        : 200
StatusDescription : OK
Content           : "<PosList WayBusinessDay=\"20240806\" WaySystemDate=\"20240807 11:54\" WayVersion=\"NP6.1.0.MR34QR0B23415\" WayPackage=\"3812019\" WayState=\"CLOSED\" WayUpdt=\"\" 
                    IsPrimaryProductionAvailable=\"true\...
RawContent        : HTTP/1.1 200 OK
                    Transfer-Encoding: chunked
                    Content-Type: application/json; charset=utf-8
                    Date: Wed, 07 Aug 2024 02:54:55 GMT
                    Server: Microsoft-HTTPAPI/2.0 Microsoft-HTTPAPI/2.0
                    
                    "<PosList WayBus...
Forms             : {}
Headers           : {[Transfer-Encoding, chunked], [Content-Type, application/json; charset=utf-8], [Date, Wed, 07 Aug 2024 02:54:55 GMT], [Server, Microsoft-HTTPAPI/2.0 Microsoft-HTTPAPI/2.0]}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : System.__ComObject
RawContentLength  : 1473

When I check the contents I can see that it is outputted as one line

"<PosList WayBusinessDay=\"20240806\" WaySystemDate=\"20240807 11:54\" WayVersion=\"NP6.1.0.MR34QR0B23415\" WayPackage=\"3812019\" WayState=\"CLOSED\" WayUpdt=\"\" IsPrimaryProductionAvailable=\"true\" IsFailoverProductionAvailable=\"true\" PromotedProductionNode=\"primary\" >\n   <Pos id=\"POS0001\" online=\"1\" operator=\" \" state=\"Closed\" type=\"FC\" businessDate=\"20240806 \" businessDayOpenedAt=\"20240806 10:09:24\" pkg=\"3812019\" version=\"NP6.1.0.MR34QR0B23415\" ip=\"10.75.15.11\" updt=\"false\" node=\"POS\" alias=\"POS01 FC \" workingMode=\"\" orderInProgress=\"false\"/>\n   <Pos id=\"POS0011\" online=\"1\" operator=\" \" state=\"Closed\" type=\"MCC\" businessDate=\"20240806 \" businessDayOpenedAt=\"20240806 10:09:24\" pkg=\"3812019\" version=\"NP6.1.0.MR34QR0B23415\" ip=\"10.75.15.11\" updt=\"false\" node=\"POS\" alias=\"POS11 DT \" workingMode=\"\" orderInProgress=\"false\"/>\n   <Pos id=\"POS0071\" online=\"1\" operator=\" \" state=\"Closed\" type=\"FC\" businessDate=\"20240806 \" businessDayOpenedAt=\"20240806 10:09:24\" pkg=\"3812019\" version=\"NP6.1.0.MR34QR0B23415\" ip=\"10.75.15.11\" updt=\"false\" node=\"FOE\" alias=\"FOE0071 \" workingMode=\"\" orderInProgress=\"false\"/>\n   <Pos id=\"RPS0300\" online=\"1\" operator=\" \" state=\"\" type=\"\" businessDate=\"  \" businessDayOpenedAt=\" \" pkg=\"\" version=\"\" ip=\"\" updt=\"\" node=\"PROD\" alias=\"RPS0300 (Primary)\" workingMode=\" \" orderInProgress=\"false\"/>\n</PosList>"

So after that I am using the split $result.Content.Split('/>') and the result become:

"<PosList WayBusinessDay=\"20240806\" WaySystemDate=\"20240807 11:54\" WayVersion=\"NP6.1.0.MR34QR0B23415\" WayPackage=\"3812019\" WayState=\"CLOSED\" WayUpdt=\"\" IsPrimaryProductionAvailable=\"true\" IsFailoverProductionAvailable=\"true\" PromotedProductionNode=\"primary\" 
\n   <Pos id=\"POS0001\" online=\"1\" operator=\" \" state=\"Closed\" type=\"FC\" businessDate=\"20240806 \" businessDayOpenedAt=\"20240806 10:09:24\" pkg=\"3812019\" version=\"NP6.1.0.MR34QR0B23415\" ip=\"10.75.15.11\" updt=\"false\" node=\"POS\" alias=\"POS01 FC \" workingMode=\"\" orderInProgress=\"false\"

\n   <Pos id=\"POS0011\" online=\"1\" operator=\" \" state=\"Closed\" type=\"MCC\" businessDate=\"20240806 \" businessDayOpenedAt=\"20240806 10:09:24\" pkg=\"3812019\" version=\"NP6.1.0.MR34QR0B23415\" ip=\"10.75.15.11\" updt=\"false\" node=\"POS\" alias=\"POS11 DT \" workingMode=\"\" orderInProgress=\"false\"

\n   <Pos id=\"POS0071\" online=\"1\" operator=\" \" state=\"Closed\" type=\"FC\" businessDate=\"20240806 \" businessDayOpenedAt=\"20240806 10:09:24\" pkg=\"3812019\" version=\"NP6.1.0.MR34QR0B23415\" ip=\"10.75.15.11\" updt=\"false\" node=\"FOE\" alias=\"FOE0071 \" workingMode=\"\" orderInProgress=\"false\"

\n   <Pos id=\"RPS0300\" online=\"1\" operator=\" \" state=\"\" type=\"\" businessDate=\"  \" businessDayOpenedAt=\" \" pkg=\"\" version=\"\" ip=\"\" updt=\"\" node=\"PROD\" alias=\"RPS0300 (Primary)\" workingMode=\" \" orderInProgress=\"false\"

\n<
PosList
"

But I am still not able to save as a table. I would like to have something like

Pos ID State Businessdate Alias
POS0001 online 20240806 POS01 FC

Is there a way to do an loop inside the contents and save it as table? I already tried convertFrom-Json and convertFrom-CSV but both failed.

Thanks a lot

→Update 1

Using Invoke-RestMethod the output is

<PosList WayBusinessDay="20240806" WaySystemDate="20240807 12:49" WayVersion="NP6.1.0.MR34QR0B23415" WayPackage="3812019" WayState="CLOSED" WayUpdt="" IsPrimaryProductionAvailable="true" IsFailoverProductionAvailable="true" PromotedProductionNode="primary" >
   <Pos id="POS0001" online="1" operator=" " state="Closed" type="FC" businessDate="20240806 " businessDayOpenedAt="20240806 10:09:24" pkg="3812019" version="NP6.1.0.MR34QR0B23415" ip="10.75.15.11" updt="false" node="POS" alias="POS01 FC " workingMode="" orderInProgress="false"/>
   <Pos id="POS0011" online="1" operator=" " state="Closed" type="MCC" businessDate="20240806 " businessDayOpenedAt="20240806 10:09:24" pkg="3812019" version="NP6.1.0.MR34QR0B23415" ip="10.75.15.11" updt="false" node="POS" alias="POS11 DT " workingMode="" orderInProgress="false"/>
   <Pos id="POS0071" online="1" operator=" " state="Closed" type="FC" businessDate="20240806 " businessDayOpenedAt="20240806 10:09:24" pkg="3812019" version="NP6.1.0.MR34QR0B23415" ip="10.75.15.11" updt="false" node="FOE" alias="FOE0071 " workingMode="" orderInProgress="false"/>
   <Pos id="RPS0300" online="1" operator=" " state="" type="" businessDate="  " businessDayOpenedAt=" " pkg="" version="" ip="" updt="" node="PROD" alias="RPS0300 (Primary)" workingMode=" " orderInProgress="false"/>
</PosList>

Solution

  • As noted in comments, use Invoke-RestMethod instead to get the XML response, then you can use the XmlDocument class to parse it and get the desired object.

    $response = Invoke-RestMethod http://SomeStoreName/npsharp/services/INP6Waycore/GetPosListXML
    $xml = [xml]::new()
    $xml.LoadXml($response)
    $xml.SelectNodes('*/Pos') | Select-Object id, state, businessDate, Alias
    

    Possibly you might be able to save the Xml parsing step if you use -ContentType application/xml or -ContentType text/xml:

    $response = Invoke-RestMethod http://SomeStoreName/npsharp/services/INP6Waycore/GetPosListXML -ContentType application/xml
    $response.SelectNodes('*/Pos') | Select-Object id, state, businessDate, Alias
    

    With the sample XML posted in question you should get something like:

    id      state  businessDate alias
    --      -----  ------------ -----
    POS0001 Closed 20240806     POS01 FC
    POS0011 Closed 20240806     POS11 DT
    POS0071 Closed 20240806     FOE0071
    RPS0300                     RPS0300 (Primary)