我使用python请求模块进行REST请求.
我正在尝试制作一个肥皂请求,但我想知道无法为此得到一个例子.这是我的肥皂身体和标题.
<auth>
<apikey>xcvzxcvcxzv-a0-0035c6fbc04f</apikey>
</auth>
Run Code Online (Sandbox Code Playgroud)
身体
<reports>
<report>
<name>Test</name>
</report>
</reports>
Run Code Online (Sandbox Code Playgroud)
这是wsdl url
https://ltn.net/webservices/booking/r1/index.wsdl
Run Code Online (Sandbox Code Playgroud)
请告诉我如何使用python在这里发布帖子请求.如果不可能使用requests
模块那么可能是其他替代方案?
Kom*_*omu 10
我最近遇到了同样的问题,不幸的是,既没有泡沫也没有jurko-suds(保持肥皂泡沫)能够提供帮助.这主要是因为suds一直生成错误格式化的肥皂信封(如果应该生成的肥皂有一些应该在CDATA内的内容),这与服务器的预期不同.即使我尝试使用__inject选项自己注射肥皂信封也是如此.
这是我如何使用python请求解决它
import requests
request = u"""<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://SOME_URL">
<soapenv:Header>
<user>{0}</user>
<pass>{1}</pass>
</soapenv:Header>
<soapenv:Body>
<req:RequestInfo><![CDATA[<?xml version='1.0' encoding='UTF-8'?><request xmlns='http://SOME_OTHER_URL'>
<Call>
<callID>{2}</callID>
...
</Call>
<Foo>
<Bar>
<Baz>{3}</Baz>
...
</Bar>
<Neytri>
...
</Neytri>
</Foo>
<Title>{4}</Title>
</request>]]></req:RequestInfo>
</soapenv:Body>
</soapenv:Envelope>""".format('Jake_Sully',
'super_secret_pandora_password',
'TYSGW-Wwhw',
'something_cool',
'SalaryPayment',
'Pandora_title',
)
encoded_request = request.encode('utf-8')
headers = {"Host": "http://SOME_URL",
"Content-Type": "application/soap+xml; charset=UTF-8",
"Content-Length": str(len(encoded_request)),
"SOAPAction": "http://SOME_OTHER_URL"}
response = requests.post(url="http://SOME_OTHER_URL",
headers = headers,
data = encoded_request,
verify=False)
print response.content #print response.text
Run Code Online (Sandbox Code Playgroud)
真正重要的是在头文件和SOAPAction中指定Content-Type.根据SOAP 1.1规范
The SOAPAction HTTP request header field can be used to indicate the intent of the SOAP HTTP request. The value is a URI identifying the intent. SOAP places no restrictions on the format or specificity of the URI or that it is resolvable. An HTTP client MUST use this header field when issuing a SOAP HTTP Request.
Run Code Online (Sandbox Code Playgroud)
SOAPAction的值通常可以在您要进行的API调用的wsdl文件中找到; 如果wsdl文件中没有,那么您可以使用空字符串作为该标头的值
另见:
归档时间: |
|
查看次数: |
31198 次 |
最近记录: |