Home > Enterprise >  Performing a XML-Request against Czech Trade Licensing Register (RŽP) in PowerShell
Performing a XML-Request against Czech Trade Licensing Register (RŽP) in PowerShell

Time:03-24

Good day for everybody. I would need to create a powershell script that will send xml with post request as multipart/form-data and receives it back. On the official site (only available in Czech) there is only this little code of html form:

<form name="frmdata" method="post" enctype="multipart/form-data" 
   action="http://www.rzp.cz/cgi-bin/aps_cacheWEB.sh">
      <input type="hidden" name="VSS_SERV" value="ZVWSBJXML">
      <input type="file" name="filename">
      <input type="submit" name="x" value="ODESLI">
</form>

Something about it you can find on GitHub.

I have this XML file with question:

<?xml version="1.0" encoding="ISO-8859-2"?> 
<VerejnyWebDotaz xmlns="urn:cz:isvs:rzp:schemas:VerejnaCast:v1" version="2.8"> 

   <Kriteria>
   <IdentifikacniCislo>27074358</IdentifikacniCislo>
   <PlatnostZaznamu>1</PlatnostZaznamu>
  </Kriteria>
 </VerejnyWebDotaz>

I tried something like this for automatization in powershell:

$url = "http://www.rzp.cz/cgi-bin/aps_cacheWEB.sh"

$MyXmlPath = "...\RZP.xml"
$fileBytes = [System.IO.File]::ReadAllBytes($MyXmlPath)
$fileEnc = [System.Text.Encoding]::GetEncoding('UTF-8').GetString($fileBytes);
$boundary = [System.Guid]::NewGuid().ToString()
$lineFeed = "`r`n"
$body = (
    "--$boundary",
    "Content-Disposition: form-data;
     name=`"VSS_SERV`"value=`"ZVWSBJXML`"",
    "--$boundary",    
    "Content-Disposition: form-data;
    name=`"filename`"; filename=`"RZP.xml`",
    Content-Type: text/xml; $fileEnc",
    "--$boundary$lineFeed"  
   ) -join $lineFeed

$nic =Invoke-WebRequest -Uri $url -Method 'Post' -Body $body 

but what I only get is “Chyba” (error) -1, what means that the request has not a (valid) XML question. But my XML must by definitely valid because, when I use it in html form above, it works perfectly.

Somebody any idea?

Sorry for my English.

CodePudding user response:

Fixed your code by comparing with powershell invoke-restmethod multipart/form-data

$url = "https://www.rzp.cz/cgi-bin/aps_cacheWEB.sh"

$MyXmlPath = "RZP.xml"
$fileEnc = Get-Content $MyXmlPath -RAW -Encoding UTF8
$boundary = [System.Guid]::NewGuid().ToString()
$body = (
    "--$boundary",
    "Content-Disposition: form-data; name=`"VSS_SERV`"",
    "Content-Type: text/plain",
    "",
    "ZVWSBJXML",
    "--$boundary",
    "Content-Disposition: form-data; name=`"filename`"; filename=`"RZP.xml`";",
    "",
    $fileEnc,
    "--$($boundary)--",
    ""  
   ) -join $lineFeed

$nic = Invoke-RestMethod -Uri $url -Method 'POST' -ContentType "multipart/form-data; boundary=$boundary" -UseBasicParsing -Body $body
$nic.OuterXml

Result looks promising, as it contains at least name and address.

  • Related