-1

I need to create a funcion that will send xml with post request and receives it back in python. 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>

So I am trying to convert it like this to python, for automatization, but it is not working:

headers = {'Content-Type':'text/xml'}
url = 'http://www.rzp.cz/cgi-bin/aps_cacheWEB.sh'

with open('dotaz1.txt') as xml:
    myobj = {   'VSS_SERV': 'ZVWSBJXML',
                'X': 'ODESLI',
                'file': xml,
             }

    x = requests.post(url, data = myobj, headers=headers)

Does somebody know what I am doing wrong?

SomeGuy
  • 97
  • 10
  • What do you mean by "it is not working"? – mzjn Dec 09 '21 at 10:44
  • Why do you pass a file pointer instead of a fileName ? – Ptit Xav Dec 09 '21 at 10:46
  • @mzjn That I get simple messege, that something went wrong, no idea what. – SomeGuy Dec 09 '21 at 10:50
  • You have this [post](https://stackoverflow.com/questions/20759981/python-trying-to-post-form-using-requests) with different possibilities – Ptit Xav Dec 09 '21 at 10:51
  • Does this answer your question? [Python : Trying to POST form using requests](https://stackoverflow.com/questions/20759981/python-trying-to-post-form-using-requests) – Ptit Xav Dec 09 '21 at 10:51
  • @Ptit Xav to a reference to this https://stackoverflow.com/questions/40140412/post-xml-file-with-requests – SomeGuy Dec 09 '21 at 10:51
  • @PtitXav unfortunetlly no – SomeGuy Dec 09 '21 at 11:05
  • In the form you do not send xml file contents but only a filename (may be I am wrong). So why do you try to read a file in the python code ? I would suggest to replace “file: xml “ by ‘fileName’: ‘dataz1.xml’ – Ptit Xav Dec 09 '21 at 11:20
  • @PtitXav input is type of file, so you are sending it. But in html file, when I removed enctype from parameters it wont work, so meabz I need to define encryption on post request? But I cant find how anywhere. – SomeGuy Dec 09 '21 at 11:39
  • Found this [post](https://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean) about encryp. It seems you have to set it. – Ptit Xav Dec 09 '21 at 17:41

2 Answers2

1

I got a valid response with the following code:

import requests

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

if __name__ == '__main__':
    with open('query.xml') as xml:
        request_data = {
            'VSS_SERV': 'ZVWSBJXML',
            'filenameList': xml
        }

        response = requests.post(url, data=request_data)
        print(f"status code: {response.status_code}")
        print(f"response:\n{response.text}")

Please note: The parameter filenameList differs from the official API documentation. However, using the described value of filename led to this very specific response from the server:

Multiple definitions of filename encountered in input. If you're trying to do this intentionally (such as with select), the variable must have a "List" suffix.

I changed the request accordingly and got a valid XML response. Further note that I do not set an explicit HTTP handler but rather let the requests module pick the appropriate headers. It seems that this works fine - as long as you use filenameList. There are mentions about this issue on the internet. Just google for the quoted error message and you will find a couple of results. It seems that by chosing exactly the correct HTTP headers you can also provide the filename attribute.

I found some help in this github repository: https://github.com/f4z4onZH27/rzp

I cannot resist resist but to quote from its readme:

So, here we are, fixing government’s and major vendor’s incompetency.

Lydia van Dyke
  • 2,466
  • 3
  • 13
  • 25
0

So I finally got the answer. I got some inspiration from this post, where there is a implementation for C# : Make POST request

As for python, trick was in sending it as a file, as it wont accept another format. Also the text variable in my code needs to be is shown format. You cant displace text from top or there will be error.

Code:

import requests

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

text = """<?xml version="1.0" encoding="UTF-8"?>
<VerejnyWebDotaz xmlns="urn:cz:isvs:rzp:schemas:VerejnaCast:v1" version="2.8">
   <Kriteria>
     <IdentifikacniCislo>75848899</IdentifikacniCislo>
     <PlatnostZaznamu>0</PlatnostZaznamu>
   </Kriteria>
</VerejnyWebDotaz>
"""


if __name__ == '__main__':
with open('query.xml', 'r+') as xml:
    xml.truncate(0)
    xml.write(text)
    xml.seek(0)
    request_data = {
        'VSS_SERV': 'ZVWSBJXML',
    }

    response = requests.post(url, data=request_data, files={'filename' : xml})
    print(f"status code: {response.status_code}")
    print(f"response:\n{response.text}")

To use it for automatization, all you need to do is to dynamiclly change ID of subject (IdentifikacniCislo)

SomeGuy
  • 97
  • 10