Jordan
Jordan

Reputation: 173

converting curl command to a Python3 requests.post (SOAP xml)

I'm trying to get the following command line to work in Python using the 'requests' library:

curl --header "Content-Type: text/xml;charset=UTF-8" --data @test_GetCapabilities.xml http://www.bom.gov.au/waterdata/services?service=SOS

the curl returns the desired response immediately when executed in Anaconda Prompt from the directory containing the test_GetCapabilities.xml file. But the post request does not work when i run the Python script below.

import requests
url = 'http://www.bom.gov.au/waterdata/services?service=SOS'
payload = "test_GetCapabilities.xml"
headers = {'Content-Type': 'text/xml', 'charset': 'UTF-8'}
r = requests.post(url, data=open(payload), headers=headers)
print(r.content)

The above code times out after about a minute and gives this stack:

runfile('C:/Python/SOAP_curl.py', wdir='C:/Python') Traceback (most recent call last):

File "", line 1, in runfile('C:/Python/SOAP_curl.py', wdir='C:/Python')

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Python/SOAP_curl.py", line 16, in r = requests.post(url, data=open(payload), headers=headers)

File "C:\ProgramData\Anaconda3\lib\site-packages\requests\api.py", line 112, in post return request('post', url, data=data, json=json, **kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\requests\api.py", line 58, in request return session.request(method=method, url=url, **kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py", line 512, in request resp = self.send(prep, **send_kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py", line 622, in send r = adapter.send(request, **kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\requests\adapters.py", line 495, in send raise ConnectionError(err, request=request)

ConnectionError: ('Connection aborted.', BadStatusLine('HTTP/1.1 0 Init\r\n'))

Here is the contents of test_GetCapabilities.xml:

<soap12:Envelope xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"
xmlns:sos="http://www.opengis.net/sos/2.0"
xmlns:wsa="http://www.w3.org/2005/08/addressing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ows="http://www.opengis.net/ows/1.1"
xmlns:fes="http://www.opengis.net/fes/2.0"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:swes="http://www.opengis.net/swes/2.0"
xsi:schemaLocation="http://www.w3.org/2003/05/soap-envelope
http://www.w3.org/2003/05/soap-envelope/soap-envelope.xsd
http://www.opengis.net/sos/2.0
http://schemas.opengis.net/sos/2.0/sos.xsd">
<soap12:Header>
<wsa:To>http://www.ogc.org/SOS</wsa:To>
<wsa:Action> http://www.opengis.net/def/serviceOperation/sos/core/2.0/GetCapabilities
</wsa:Action>
<wsa:ReplyTo>
<wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address>
</wsa:ReplyTo>
<wsa:MessageID>0</wsa:MessageID>
</soap12:Header>
<soap12:Body>
<sos:GetCapabilities service="SOS"/>
</soap12:Body>
</soap12:Envelope>

Upvotes: 0

Views: 1770

Answers (1)

Jordan
Jordan

Reputation: 173

Thanks @kcorlidy.

Here is the corrected working code:

import requests 
url = 'http://www.bom.gov.au/waterdata/services?service=SOS' 
payload = "test_GetCapabilities.xml" 
headers = {'Content-Type': 'text/xml', 'charset': 'UTF-8'} 
with open(payload) as fd: 
    r = requests.post(url, data=fd.read().replace("\n",""), headers=headers) 
print(r.content); 

Upvotes: 0

Related Questions