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?
CodePudding user response:
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.